Selenium 2 - Can findElement(By.xpath) be scoped to a particular element?

后端 未结 5 1106
伪装坚强ぢ
伪装坚强ぢ 2021-01-11 12:21

All the examples of findElement(By.xpath) I\'ve seen search the whole page, e.g.

WebElement td = driver.findElement(By.xpath(\"//td[3]\"));

5条回答
  •  隐瞒了意图╮
    2021-01-11 13:03

    WebElement td = tr.findElement(By.xpath("/td[3]"));

    If you only want to to find the child elements of the tr use a relative path not an absolute.

    This should work:

    int index = 3;
    List tds = tr.findElements(By.xpath(".//td"));
    System.out.println(tds[index].getText());
    

提交回复
热议问题