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:
Then get the parent element:
Then check the parent element for an
<img>tag: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:
then just pass in TEST1 or TEST2