简体   繁体   中英

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. I know that I can find an a -tag with partial link like findElement(By.partialLinkText("/ResetPassword/")); (the number 2 can change, so I can´t use it as seperator). But I need to seperate it by User. Is there a solution like tr.td.text("User2") > findElement(By.partialLinkText("/ResetPassword/")); ?

This XPath should do the trick for you. .//tr[td[normalize-space(text())='User2']]//a Just keep changing "User2" part with the desired user value.

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". 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.

I hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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