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

后端 未结 5 1206
伪装坚强ぢ
伪装坚强ぢ 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 12:56

    I think this might be a bug in the way that Selenium2 uses xpath. However, I believe I've successfully limited the scope using "::ancestor" before.

    In any event, have you tried using Css Selectors to solve this? Is that an option for you? Try this:

    tr.findElement(By.cssSelectors("td:nth-of-type(3)"));

    This should do the job and is the equivalent of what you tried initially:

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

提交回复
热议问题