EDIT:
public bool getImage()
{
IWebElement table = driver.FindElement(By.Id(\"DIV_ID_1\"));
string name = String.
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