我正在尝试使用通配符在 selenium 中向上导航 DOM,使用类似这样的东西......
ArrayList<WebElement> list = (ArrayList<WebElement>) driver.findElements(By.xpath("//div[contains(text(),'div_text')]/parent::*]"));
尝试获取具有“div_text”的 div 的所有父元素。这似乎只返回直接父级,而不是所有父级。我需要在 DOM 上移动可变数量的节点。
一旦我得到列表,我计划迭代
for(WebElement e: list)
if(e.getTagName().equals("table") && e.getAttribute("class").equals("blah")
e.click();
同意这不是最有效的方法,而是更复杂的 xpath 表达式,例如
driver.findElements(By.xpath("//div[text()='title']/[name(parent::*) = 'table'"));
似乎不适用于网络驱动程序。
感谢您对此的任何见解。
最佳答案
如果我正确理解你的问题,你想要的是祖先而不是 parent 。
ancestor: Selects all ancestors (parent, grandparent, etc.) of the current node parent
parent: Selects the parent of the current node
尝试 //div[text()='title']/ancestor::table
作为表祖先
关于java - 在 Selenium Web 驱动程序中使用 Parent::* 轴时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16576344/