EDIT:
public bool getImage()
{
IWebElement table = driver.FindElement(By.Id("DIV_ID_1"));
string name = String.Format("//*[contains(text(), \'{0}\')]", 'TEST1');
IWebElement element = table.FindElement(By.XPath(name));
IWebElement parent = element.FindElement(By.XPath(".."));
try
{
IWebElement image = element.FindElement(By.XPath("//img"));
}
catch (NoSuchElementException e)
{
return false;
}
return true;
}
How would I find out if the TEST1 does have Image? in the below html source code, I have tr and within tr i have td and some tr may have image tag and some may not
So, I will be passing name for an example: TEST1 and in returns I will be expecting if the name has Image tag or not.
again, if I pass TEST2 and TEST3 it should return null since it does not have an image tag and where as TEST1 and TEST4 does have Image tag hence it should return me true.
I tried something like this but did not work:
string name = String.Format(".//td[contains(., \'{0}\')]/..//#src", "TEST1");
IWebElement element = driver.FindElement(By.XPath(name));
get this error: after trying the above code...
The xpath expression './/td[contains(., 'TEST1')]/..//#src'
cannot be evaluated or does notresult in a WebElement
Below is the html source code
<div id="DIV_ID_1">
<table id="TBLID1">
<tr>
<td>
TEST1
</td>
<td>
<img id="ctl00" src="../App_Themes/Default/images/phone.gif" />
</td>
</tr>
<tr>
<td>
TEST2
</td>
</tr>
<tr>
<td>
TEST3
</td>
</tr>
<tr>
<td>
TEST4
</td>
<td>
<img id="ctl02" src="../App_Themes/Default/images/phone.gif" />
</td>
</tr>
</table>
</div>
So I would break this up into a few steps.
First get your element:
WebElement element = driver.findElement(By.xpath("//*[contains(text(), 'TEST1')]"));
Then get the parent element:
WebElement parent = element.findElement(By.xpath(".."));
Then check the parent element for an <img> tag:
Try
{
WebElement image = parent.findElement(By.xpath("//img"));
}
catch (NoSuchElementException e)
{
System.out.println("Did not find an image");
}
I'd wrap this in a function that I could then pass in the text to find the image and return the element if it exists.
Something like:
public WebElement getImage(String innerText)
then just pass in TEST1 or TEST2
Using your original XPath, it works fine, executed both in Firefox and Chrome's developer tools and it returns the img. Does it actually return anything or just doesn't return what you'd expect? Does it error saying the element cannot be found?
This is another way of getting it:
//td[normalize-space(text())='TEST1']/../descendant::img/#src
Related
Element structure
//table[#class='table-table-condensed']//tbody//tr...
<td class="ng-binding">TEXT1</td>
<td class="ng-binding">2020-04-17 18:55:58.022</td>
<td>
<span class="label label-default">Not Started</span>
</td>
<td>
<div class="pull-right">
<button class="btn-success"/>
<button class="run"/>
<button class="review"/>
</div>
</td>
Trying to access the button element :
If Text Matches for this Element :
WebElement we = driver.findElement.By(xpath("//table[#class='table-table-condensed']//tbody//tr//td[#class='ng-binding'][1]");
String str = we.text();
if(str.equals("TEXT1"))
{
WebElement ex = we.findElement(By.xpath("./parent::following:://button[#class='run']"));
ex.click();
}
this throws no element found exception, Tried following::sibling too. Can we use both the tags at a time here. How to access the button element.
Also, will following:: tag does not work, if I had to access the below tags in structure:
<td class ="ng-binding">2020-04-17 18:55:58.022</td> ( Need to extract this element to sort the list by Latest date)
AND
<span class ="label label-default">Not Started</span>
Try below solution
wait = WebDriverWait(driver, 10)
elementText= wait.until(EC.element_to_be_clickable((By.XPATH, "//table[#class='table-table-condensed']//tbody//tr/following-sibling::td[contains(text(),'Test1')][contains(#id, "ng-binding")]")))
button=wait.until(EC.element_to_be_clickable((By.XPATH, "//button[#class='run']")))
if(elementText.text=="Test1"):
button.click()
else:
print "Not found!"
Your ./parent::following:://button[#class='run'] is not valid XPath!
The correct form is: axis::node[predicate]. You are not allowed to chain multiple axis together, and you must specify a node! Note that a wilcard * node is still valid.
What you are probably looking for is ./following-sibling::td//button[#class='run'] or possibly ./following::button[#class='run'].
Other approach, you can try to solve this by using tr[contains(., 'TEXT VALUE')]:
String text = "TEXT1";
WebElement element1 = driver.findElement(By.xpath("//table[#class='table-table-condensed']//tr[contains(., '" +text +"')]//td[2]"));
WebElement element2 = driver.findElement(By.xpath("//table[#class='table-table-condensed']//tr[contains(., '" +text +"')]//span"));
WebElement element3 = driver.findElement(By.xpath("//table[#class='table-table-condensed']//tr[contains(., '" +text +"')]//button[#class='run']"));
System.out.println(element1.getText());
System.out.println(element2.getText());
element3.click();
I am trying to finding xpath for image.below is my code.i am getting error unable to locate the element.
driver.findElement(By.xpath("//img[#src='./pics/logo /home.jpg']")).click();
Below mention is my table code. from where i am trying to find xpath for image.
<table cellspacing="0" cellpadding="0" width="600" border="0">
<tbody>
<tr>
<tr>
<td style="vertical-align: top;padding-top:10px;padding-right: 3px;">
<td width="30%" style="vertical-align: top;padding-top:10px;">
<a title="Access to Data (S,g,...)" target="_top" href="./action/updateTabs?tabSet=requestId=1457516682135">
<img border="0" src="./pics/logo/home/EMLogoMini.jpg">
</a>
</td>
I have observed that there is a space in your xpath and the URL is different too..
Use below code:-
driver.findElement(By.xpath("//img[#src='./pics/logo/home/EMLogoMini.jpg']")).click();
Or use cssSelector as below :-
driver.findElement(By.cssSelector("img[src='./pics/logo/home/EMLogoMini.jpg']")).click();
List<WebElement> list=driver.findElements(By.xpath("//img[#src='./pics/logo/home/EMLogoMini.jpg']"));
for(WebElement e : list){
e.click();
}
How to click by different ways:-
If your problem is that the element is scrolled off the screen (and as a result under something like a header bar), you can try scrolling it back into view like this:
private void scrollToElementAndClick(WebElement element) {
int yScrollPosition = element.getLocation().getY();
js.executeScript("window.scroll(0, " + yScrollPosition + ");");
element.click();
}
if you need you could also add in a static offset (if for example you have a page header that is 200px high and always displayed):
public static final int HEADER_OFFSET = 200;
private void scrollToElementAndClick(WebElement element) {
int yScrollPosition = element.getLocation().getY() - HEADER-OFFSET;
js.executeScript("window.scroll(0, " + yScrollPosition + ");");
element.click();
}
If still not work then use JavascriptExecutor
WebElement element= driver.findElement(By."Your Locator"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
Hope it will help you :)
try to remove spaces and be sure that the URL is same...
I want to find the the element for the Edit:
my java code is not working.
String xpathLocater = "//a[contains(text(),'onEditFilter('modifier')')]";
return driver.findElement(By.xpath(xpathLocater));
This is the source code for the element.
<tr class="listeven">
<td>
<a onclick="return onEditFilter('modifier');" href="#">Edit</a>
</td>
</tr>
something like
String xpathLocater = "//a[contains(#onclick,\"onEditFilter('modifier')\")]"
How to retrieve the text 'caught jake' from the below code using selenium webdriver?
Am able to point to that text using the below xpath but am unable to print the text. :(
//*[#id='full-scorecard']/div[2]/div/table[1]/tbody/tr[3]/td[2]/child::text()
<div class="row">
<div class="large 20 columns">
<table class="batting-table innings" width="100%">
<tbody>
<tr class="tr-heading">
<tr>
<tr class="dismissal-detail" style="display: table-row;">
<td width="2%" />
<td colspan="8">
<b>12.6</b> caught Jake
<b>73/4</b>
<br/>
Using java you can retrieve from following lines:
String text = driver.findElement(By.xpath("//div[#class='large 20 columns']")).getText();
System.Out.print("Text= "+text);
Above line will return all inner tags string's of div element.
If you want to get only 12.6 text, then use below lines:
String text = driver.findElement(By.xpath("//table[#class='batting-table innings']/tr/tr/tr/td[2]/b")).getText();
System.Out.print("Text= "+text);
And be sure that there should be single element which has this xpath 'By.xpath("//table[#class='batting-table innings']/tr/tr/tr/td[2]/b")', otherwise you will get an exception.
depend on your language, you can use following xpath expression:
//div[#class='large 20 columns']/table/tbody/tr/tr/td[2]/child::text()
full Java code:
JavascriptExecutor js = (JavascriptExecutor) driver;
String jsx = "return document.evaluate(\"//*[#id='full-scorecard']/div[2]/div/table[1]/tbody/tr[3]/td[2]/child::text()\", document, null, XPathResult.STRING_TYPE, null).stringValue;";
String objList = js.executeScript(jsx);
System.out.println( (String) objList);
I need to getAttribute("src") of the image. When I run my script, it shows "null". BTW, action works!
My script:
WebElement image = driver.findElement(By.id("creativeLink"));
verifyDisplay(image.getAttribute("src"), By.id("creativeLink"));
action.moveToElement(image).perform();
Page HTML:
<div id="creativeContent">
<table>
<tbody><tr>
<td>
<a id="creativeLink" href="http://www.website.com" target="_new"> <img href="http://www.website.com" rel="faceboxMO" alt="" src="https://console.data.com/images/login/logo.png" height="50" border="0" width="50"></a>
</td>
<td valign="top"><div class="hint">(Note: Mouse over creative to see in correct dimensions)</div></td>
</tr>
<tr>
<td>
</td></tr></tbody></table>
</div>
Looks like you are using wrong selector there. Since you are using some custom function I was not able to test it. However, I came up with something like following and seems working. A correct selector should look like something as following:
// The selector finds the img element
By by = By.cssSelector("#creativeLink>img");
//Getting attribute on on img tag
String src = driver.findElement(by).getAttribute("src");
System.out.println(src);
Print
https://console.data.com/images/login/logo.png
Probably do something like this and it will work:
WebElement image = driver.findElement(By.cssSelector("#creativeLink>img"));
verifyDisplay(image.getAttribute("src"), By.cssSelector("#creativeLink>img"));
action.moveToElement(image).perform();