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.
Related
hello i need little help reading website content
i want to read
<tr>
<td class="text-center"><strong>This Month</strong></td>
<td class="text-center">1194</td>
<td class="text-center">22</td>
<td class="text-center">7</td>
</tr>
i make it like this but it always return nothing
if (url.toLowerCase().contains("top100arena.com") && line.contains("<strong>This Month</strong></td><td class=\"text-center\">"))
return Integer.valueOf(line.split(">")[1].replace("</td", "").replace(",", ""));
if you are considering using third-party libraries, jsoup is a good idea.
https://jsoup.org/
using css / xpath selectors, you can indicate the element you are interested in, e.g.
//tr/td[contains (strong,.)]
will find all bold entries, then we can get the parent for that element, and read all the elements
http://xpather.com/
https://devhints.io/xpath
jsoup: How to select the parent nodes, which have children satisfying a condition
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']")));
I'm newby in selenium automation.
I have one scenario where I have to click on a button based on some title. but the button element looking same for all all rows.
This is the image:
this is html for the same-
<tr class="odd gradeX">
<td class="hide"></td>
<td>My sample Test</td>
<td> Priyank pareek </td>
<td style="width:50px;text-align:center;">01/11/2017</td>
<td style="width:50px;text-align:center;"> Approved </td>
<td style="width:50px;text-align:center;">
<td style="width:100px;">
<a class="btn btnReject red btn-sm btn-outline sbold uppercase" href="javascript:;" onclick="updateTest(this)" data-status="0" data-gui="6438C99F-E166-49DA-8B89-DD0E2EF33A62" title="Reject">
</td>
</tr>
How can i click on the reject button ? please help me
You can use below XPath:
WebElement element = driver.findElement(By.xpath("//a[#title='Reject']"));
or CSS selector
WebElement element = driver.findElement(By.cssSelector("a[title='Reject']"));
If these selectors still matches multiple elements, try:
WebElement element = driver.findElement(By.xpath("//tr[td[text()='My sample Test']]//a[#title='Reject']"));
Try to locate using following xpath -
//td[text()='My sample Test']/following-sibling::td/a[#title='Reject']
Explanation :-
Find the td tag which having text as 'My sample Test'
following-sibling used to navigate the sibling which have reject button
You can search using class name btnReject. In Python, I usually use driver.getElementByClassName("btnReject").click(). You can change them into Java to use.
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
I want to find the element of this link "us states" in <h5>. I am trying this on Craigslist. How can I do it?
Here is the URL: http://auburn.craigslist.org/
<html class="">
<head>
<body class="homepage w1024 list">
<script type="text/javascript">
<article id="pagecontainer">
<section class="body">
<table id="container" cellspacing="0" cellpadding="0"
<tbody>
<tr>
<td id="leftbar">
<td id="center">
<td id="rightbar">
<ul class="menu collapsible">
<li class="expand s">
<li class="s">
<li class="s">
<h5 class="ban hot">us states</h5>
<ul class="acitem" style="display: none;">
</li>
<li class="s">
<li class="s">
Only using class names is not sufficient in your case.
By.cssSelector(".ban") has 15 matching nodes
By.cssSelector(".hot") has 11 matching nodes
By.cssSelector(".ban.hot") has 5 matching nodes
Therefore you need more restrictions to narrow it down. Option 1 and 2 below are available for CSS selector, 1 might be the one that suits your needs best.
Option 1: Using list items' index (CssSelector or XPath)
Limitations
Not stable enough if the site's structure changes
Example:
driver.FindElement(By.CssSelector("#rightbar > .menu > li:nth-of-type(3) > h5"));
driver.FindElement(By.XPath("//*[#id='rightbar']/ul/li[3]/h5"));
Option 2: Using Selenium's FindElements, then index them. (CssSelector or XPath)
Limitations
Not stable enough if a site's structure changes
Not the native selector's way
Example:
// Note that By.CssSelector(".ban.hot") and //*[contains(#class, 'ban hot')] are different, but doesn't matter in your case
IList<IWebElement> hotBanners = driver.FindElements(By.CssSelector(".ban.hot"));
IWebElement banUsStates = hotBanners[3];
Option 3: Using text (XPath only)
Limitations
Not for multilanguage sites
Only for XPath, not for Selenium's CssSelector
Example:
driver.FindElement(By.XPath("//h5[contains(#class, 'ban hot') and text() = 'us states']"));
Option 4: Index the grouped selector (XPath only)
Limitations
Not stable enough if the site's structure changes
Only for XPath, not CssSelector
Example:
driver.FindElement(By.XPath("(//h5[contains(#class, 'ban hot')])[3]"));
Option 5: Find the hidden list items link by href, then traverse back to h5 (XPath only)
Limitations
Only for XPath, not CssSelector
Low performance
Tricky XPath
Example:
driver.FindElement(By.XPath(".//li[.//ul/li/a[contains(#href, 'geo.craigslist.org/iso/us/al')]]/h5"));
By.cssSelector(".ban") or By.cssSelector(".hot") or By.cssSelector(".ban.hot") should all select it unless there is another element that has those classes.
In CSS, .name means find an element that has a class with name. .foo.bar.baz means to find an element that has all of those classes (in the same element).
However, each of those selectors will select only the first element that matches it on the page. If you need something more specific, please post the HTML of the other elements that have those classes.
You can describe your CSS selection like cascading style sheet rows:
protected override void When()
{
SUT.Browser.FindElements(By.CssSelector("#carousel > a.tiny.button"))
}