ImageTag exists in the TR?

匿名 (未验证) 提交于 2019-12-03 02:31:01

问题:

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>

回答1:

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



回答2:

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


转载请标明出处:ImageTag exists in the TR?
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!