简体   繁体   English

Selenium WebDriver Java / JUnit-“获取表”元素

[英]Selenium WebDriver Java / JUnit - Getting table element

I have the following table: 我有下表:

<table>
    <tbody>
        <tr>
            <td>
                <a href="/ResetPassword/1/"></a>
            </td>
            <td>
                User1
            </td>
        </tr>
        <tr>
            <td>
                <a href="/ResetPassword/2/"></a>
            </td>
            <td>
                User2
            </td>
        </tr>
    </tbody>
</table>

I want to find the a -tag of the tr, where the data User2 is in the same row. 我想找到tr a -tag,其中数据User2在同一行中。 I know that I can find an a -tag with partial link like findElement(By.partialLinkText("/ResetPassword/")); 我知道我可以找到带有部分链接a -tag,例如findElement(By.partialLinkText("/ResetPassword/")); (the number 2 can change, so I can´t use it as seperator). (数字2可以更改,因此我不能将其用作分隔符)。 But I need to seperate it by User. 但是我需要按用户分开。 Is there a solution like tr.td.text("User2") > findElement(By.partialLinkText("/ResetPassword/")); 是否有类似tr.td.text("User2") > findElement(By.partialLinkText("/ResetPassword/")); ?

This XPath should do the trick for you. 这个XPath应该可以帮您解决问题。 .//tr[td[normalize-space(text())='User2']]//a Just keep changing "User2" part with the desired user value. .//tr[td[normalize-space(text())='User2']]//a只需不断用所需的用户值更改“ User2”部分即可。

U can try something like this(not sure though)- 你可以尝试这样的事情(虽然不确定)-

List<WebElement> list=table.findElements(By.tagName("tr"));
List<WebElement> tdvalues=null;
for(WebElement web:list){
   tdvalues=web.findElements(By.tagName("td"));
      if(tdvalues.contains("User2")){
          System.out.println(tdvalues.get(0).getText());//0th position contains the      link
      }
tdvalues.clear();
} 

Hi I was wondering if you could use the .getValue() statement with /@a at the end of the xpath to locate the attribute "a". 嗨,我想知道您是否可以在xpath的末尾使用.getValue()语句和/@a来查找属性“ a”。 Main thing to do is find a bulletproof xpath to locate the User2 row once you have done that finding the value of "a" should be easy enough. 主要要做的是一旦找到“ a”的值应该很容易就找到了防弹xpath来定位User2行。

I hope this helps 我希望这有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM