How to access nowrap element in selenium - java

I'm trying to access the first input field from the below HTML, but unable to access the field. Find the HTML details as below:
<tr>
<td>....</td>
<td nowrap>
<input type="text" name="details" id="details" value size="10" onfocus="doFieldFocusEvent(event)">
<input type="hidden" id=details1 value>
</td>
I have tried with the below approach but all returns false.
driver.findElement(By.xpath("//input[#id='details']"));
driver.findElement(By.xpath("//input[#name='details']"));
driver.findElement(By.id("details"));
driver.findElement(By.name("details"));
Can anyone help me to come out from this. Also can anyone give me idea on nowrap value.

I think a lot of relevant information got trimmed while you modified the actual HTML to present the the dummy HTML within the question. However a quick look at the attribute onfocus="doFieldFocusEvent(event)" confirms that the HTML DOM contains Javascript / Ajax Calls.
HTML nowrap Attribute
HTML nowrap Attribute attribute is a boolean attribute and when present it specifies that the content within the <td> cell should not wrap.
In the snapshot below the <td> element with nowrap attribute doesn't gets wrapped up while the <td> element without nowrap attribute gets wrapped up in absence of proper width/space.
Main Issue
I suspect your main issue is purely synchronization issue for which you have to induce WebDriverWait.
Solution
While you lookout for this particular <td> element induce WebDriverwait in-conjunction with ExpectedConditions clause as elementToBeClickable as follows :
WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='details']")));

Related

How to find the respective element using xpath contains() function -selenium webdriver

This is the html snippet
<div class="ext-cal-wk-ct">
<table>
<tbody>
<tr>
<td><div>13</div></td>
<td><div>14</div></td>
<td><div>15</div></td>
<tr>
<tr>
<td><div>Calender note WebElement[no 13]</div></td>
<td><div>Calender note WebElement[no 14]</div></td>
<td><div>Calender note WebElement[no 15]</div></td>
</tr>
</tbody>
</table>
</div>
I want to find the respective element using xpath by the element no
for example:
if I use contains(text(),'13')--> WebElement no 13 should be returned
for contains(text(),'14')--> WebElement no 14 should be returned and so on
I have used two xpaths to find the elements individually but don't know how to connect them
//div[#class='ext-cal-wk-ct']/table[2]/tbody/tr/td/div[contains(text(),'13')]
//div[#class='ext-cal-wk-ct']/table[2]/tbody/tr/td/div[contains(text(),'Tim Cook')]
need some help on this
I want a xpath where I will pass the date and it will return me the calender note for that particular date
basically, contains is for partial matching.
When you write
contains(text(),'13')
there are 2 matching nodes,
<div>13</div>
and
<div>WebElement no 13</div>
so I would suggest to use :
//div[#class='ext-cal-wk-ct']/table[2]/tbody/tr/td/div[text(),'13']
to locate
<div>13</div>
or
//div[#class='ext-cal-wk-ct']/table[2]/tbody/tr/td/div[text()='WebElement no 13']
to locate this :
<div>WebElement no 13</div>
You can use below syntax by using contains in xpath
//tagname[contains(text(),'textname')]
I guess in your case you should consider exact match :
<td><div>15</div></td> : for this element
xpath => //td/div[text()='13']
and for this element : <td><div>WebElement no 13</div></td>
xpath => //td/div[text()='WebElement no 13']
i hope there is unique text in dom for given html snippet.

How to extract the dynamic values of the id attributes of the table elements using Selenium and Java

I have a table where each row will have a download link with a (partly) auto-generated id element. The reason for this is that the actual href-element will allways be "#", so the id's separate the downloads.
I need to find the name of that id element in the td. That is: I know the table row has an id element, and I know part of the name, and I need to get the exact name.
I'm accessing each row one at a time, so I only need to look within ONE td at a time. No need to look through the whole table.
I know well how to find an element when I know the name. But to find the element when I only know the type is another thing.
...
<tr>
<td class="journalTable-journalPost"
<a class="htext-small" href="#" id="downloadJournalPost-345">Download</a>
</td>
</tr>
<tr>
<td class="journalTable-journalPost"
<a class="htext-small" href="#" id="downloadJournalPost-346">Download</a>
</td>
</tr>
...
I cannot find any method(s) in the webdriver that lets me find an element by type.
Partial name would work, since the id's get the name "downloadJournalPost-xxx", where only xxx changes. But link text is the only value I can find which lets me search for a partial match.
EDIT: More complete markup.
<td class="journalTable-journalpost">
<span class="hb-tekst--sekundar">In <!----><est-ikon class="ng-star-inserted">
<div aria-hidden="true" class="hb-ikon hb-ikon--pil3-inn ">
<svg focusable="false">
<use xlink:href="#ikon-pil3-inn"></use>
</svg>
</div></est-ikon><!----></span>
<span class="hb-tekst--mellomTittel hb-avstandIngen"> Application and attachments</span>
<a class="hb-tekst--liten" href="#" id="lastNedJournalPost-2892">Download journal post</a>
</td>
Until you find the element first, you can't retrieve the attribute values of it.
Use findElements method to fetch all links using the following locator
table tr td[class='journalTable-journalPost'] a
Then iterate through each element using for-each to fetch id for each element.
Sample code:
List<WebElement> listOfLinks = driver.findElements(By.cssSelector("table tr td[class='journalTable-journalPost'] a"));
for(WebElement link: listOfLinks) {
System.out.println("id:" + link.getAttribute("id"));
}
To print the List of id attribute of the element you need to induce WebDriverWait for the visibilityOfAllElementsLocatedBy() and you can use Java8 stream() and map() and you can use either of the following Locator Strategies:
cssSelector:
List<String> myID = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("td.journalTable-journalPost>a.htext-small"))).stream().map(element->element.getAttribute("id")).collect(Collectors.toList());
System.out.println(myIDs);
xpath:
List<String> myIDs = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//td[#class='journalTable-journalPost']/a[#class='htext-small' and text()='Download']"))).stream().map(element->element.getAttribute("id")).collect(Collectors.toList());
System.out.println(myIDs);

Selenium, Java. Need to select ancestor element inside table by Xpath

I have an HTML page containing the following code :
<table class="report" style="width:100%">
<tbody>
<tr>
<th/>
<th>Position Open
<br>
<span class="timestamp">27/7/2016 16:12:12</span>
</br>
</th>
<th>Position closed
<br>
<span class="timestamp">27/7/2016 16:12:42</span>
</br>
</th>
</tr>
<tr>
<td>
<span dir="ltr">EURJPY</span>
</td>
<td>116.098</td>
<td>116.156</td>
</tr>
</tbody>
</table>
On this page I have another table with the same class attribute "report" but only this table contains texts "Position Open" and "Position Closed".
I need to select elements containing the "EURJPY", "116.098" and "116.156" data.
These elements content is changing i.e. instead of "EURJPY" may appear "EURUSD" or "GBPCAD" etc.
I tried the following code:
driver.findElement(By.xpath("//span[text()='Position Open']/ancestor::table[#class='report'](//tr)[2]/td/span")).getAttribute("textContent");
to get the first required field text but got the Invalid selector error.
Your XPath is close but there were a couple issues.
//span[text()='Position Open']/ancestor::table[#class='report'](//tr)[2]/td/span
You are searching for a SPAN that contains the text 'Position Open' when in fact it is a TH that contains the text.
//th[text()='Position Open']/ancestor::table[#class='report'](//tr)[2]/td/span
(//tr) should be corrected to //tr
//th[text()='Position Open']/ancestor::table[#class='report']//tr[2]/td/span
What you want is the text contained in the TD, not the SPAN. If you pull the text from the TD you can get the text you want from all three elements. If you pull the SPAN, then you will also need to pull the last two TDs. This way is just simpler.
...and finally, the TH contains more than just the text you are looking for. Use .contains() to get a match.
//th[text()='Position Open']/ancestor::table[#class='report']//tr[2]/td
So we take that XPath and put it into Java code and we get the below.
List<WebElement> tds = driver.findElements(By.xpath("//th[contains(text(),'Position Open')]/ancestor::table[#class='report']//tr[2]/td"));
for (WebElement td : tds)
{
System.out.println(td.getText());
}
There can be issues matching the text sometimes, use contains instead, try this selector
//th[contains(.,'Position')]/ancestor::table[#class='report']//tr[2]/td/span
You can use this xpath to locate the 3 <td> tags you are interest in
//th[contains(text(),'Position Open')]/ancestor::table//tr[2]/td
Using it will give you list of three elements, you can extract the text from them
List<WebElement> tds = driver.findElement(By.xpath"//th[contains(text(),'Position Open')]/ancestor::table//tr[2]/td");
String currency = tds.get(1).getText(); // this will be EURJPY
tds.get(2).getText(); // 116.098
tds.get(3).getText(); // 116.156

How to get the xpath of a <b> tag which is designed below the div text

I would like to fetch a text content from a tag which is located just below a div text named ,'Authors:'.
I have identified the location for 'Authors' but I need to traverse through each block of authors and need to fetch the corresponding text associated with 'Authors' label.I have tried the following but I couldn't get the result.
//table[#id='thdListTable']//td[#class='msgHead']//div[contains(text(), 'Author:')]
//td[#class='msgHead']/div[contains(text(), 'Author:')]
<table id="thdListTable" width="95%">
<tbody>
<tr>
<td class="msgHead">
<div>
Author:
<b>Kurt Wendt <Kurt_Wendt (AT) globetax D.O.T com></b>
<br/>
Subject:
<b>To Impersonate on MS SQL</b>
-
<a target="_new" href="/archives/msg/501727">Link</a>
<br/>
Posted:
<b>2015-10-14 11:31:56</b>
<br/>
</div>
</td>
</tr>
//table[#id='thdListTable']//div/b[1]"
you can try to go for the first b element that is found below your div, because that should be the author's name, if I understood you correctly.
Then just get the text via getText() method
string xpathExpression = "//td[#class='msgHead']/div[contains(text(), 'Author:')]/b[1]";
string authorName = driver.findElement(By.xpath()).getText();
yes you can reach to there by using the code
//td[#class='msgHead']/div[contains(text(), 'Author:')]/b[1]
So you can retrieve the same results for others by changing the text contain name.

I want to find the xpath for the image

This is the html code
<div class="navBg">
<table id="topnav" class="navTable" cellspacing="0" cellpadding="0" style="-moz-user- select: none; cursor: default;">
<tbody>
<tr>
<td class="logoCell" valign="top">
<td class="separator">
<td class="navItem relative" style="z-index: 99">
<td class="separator">
<td class="navItem relative">
<a class="content tasks" style="border-width: 0" href="/tasks/otasklist.do">
<div class="label" style="z-index:155; ">Tasks</div>
<img class="sizer" width="84" height="93" src="/img/default/pixel.gif? hash=1106906246"/>
<span class="bottomBorder">
I am trying to find the xpath for the image-->
src="/img/default/pixel.gif?hash=1106906246"
I have tried different combinations e:g
//table/tbody/tr/td[5][#class='navItem relative']/a/div[2]/img
I have written the following code too.
WebDriverWait wait= new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Tasks")));
driver.findElement(By.xpath("//table/tbody/tr/td[5][#class='navItem relative']/a/div[2]/img")).click();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
It's identifying the element on web page by firepath but after running the script it's not clicking on the element and the console shows "No Such Element Exception".
Please answer in java lang only.
Can somebody please help me out.???
Thx
I see you are using Selenium. The safest bet is to find the closest parent with an #id attribute, and work your way down from there. Try this: //table[#id='topnav']//img. As alecxe pointed out, depending on how unique the image is in this table, you may need to narrow the XPath down a little more. Something like //table[#id='topnav']//tr[1]//img, or even //table[#id='topnav']//td[contains(#class, 'navItem')]//img.
The XPath you posted will not work, as it has some problems compared to the sample HTML you posted:
tbody may not appear in all browsers
the qualifier [#class='navItem relative'] for the element td[5] is redundant (although this is not exactly a problem)
div[2] does not exists, your HTML sample shows only one div
There are multiple ways to find the img tag. Depending on the uniqueness of the img tag attributes and it's location on the page.
Here's one way to find it, based on the Tasks div:
//table//div[text()='Tasks']/following-sibling::img
You can also rely on the td in which the img is located and check for the sizer class:
//table//td[contains(#class, 'navItem')]/img[#class='sizer']
And so on.

Categories