【发布时间】:2016-01-06 06:47:23
【问题描述】:
driver.findElement(By.xpath(".//div[contains(text(),'Approval Date')]")).click();
上面找到表格上的第一个元素。
表格中有三行带有文本“批准日期”。
如何选择第三行而不是默认选择第一行?
【问题讨论】:
driver.findElement(By.xpath(".//div[contains(text(),'Approval Date')]")).click();
上面找到表格上的第一个元素。
表格中有三行带有文本“批准日期”。
如何选择第三行而不是默认选择第一行?
【问题讨论】:
在初始 xpath 表达式之后添加[last()] 位置索引谓词括在括号中*,以仅获取最后匹配的元素原始 xpath :
(.//div[contains(text(),'Approval Date')])[last()]
"另外,如何选择所有包含'Approval Date'的元素"
使用findElements() - 带有复数's'- 来查找选择器匹配的所有元素。
*) 为什么需要括号的解释:How to select specified node within Xpath node sets by index with Selenium?
【讨论】:
如果你想要“3rd”,正如你所说,那么你可以去[3]
driver.findElement( By.xpath(".//div[contains(text(),'Approval Date')][3]")).click();
例如,如果您想要“最后一个”条目,则选择 [last()]
driver.findElement( By.xpath(".//div[contains(text(),'Approval Date')][last()]")).click();
在你的情况下,两者都会返回相同的结果......
关于您的问题“如何选择包含“批准日期”的所有元素:
List<WebElement> elements = driver.findElements( By.xpath(".//div[contains(text(),'Approval Date')]"));
【讨论】: