我试图使用不是固定长度的xpath从列表中获取特定的web元素。
我使用的xpath是//*[@id="node-1"]/td[4],其中“节点”可以从1到任何数字。xpath中的其他所有内容都保持不变。
例如,我感兴趣的元素可以在xpath中找到如下所示:
//*[@id="node-1"]/td[4]
//*[@id="node-2"]/td[4]
//*[@id="node-3"]/td[4]
//*[@id="node-4"]/td[4]但是列表可以有不同数量的节点。
我需要生成一个for循环(在java中),它从“节点-1”开始抓取xpath中的每个webelement到有多少个节点,然后将这些元素放入列表中供以后使用。
发布于 2019-07-26 15:24:36
将这些元素放入列表中供以后使用。
以下是简单的方法。有供以后使用的清单-
List<WebElement> nodelist = driver.findElements(By.xpath("//*[starts-with(@id,'node-')]"));
for (int i = 0; i < nodelist.size(); i++) {
WebElement node = driver.findElement(By.xpath("//*[@id='node-" + Integer.toString(i) + "']"));
// do your logic here
}nodelist是供以后使用的列表。
发布于 2019-07-26 14:58:56
这是一种方法。
int numberOfNodes = driver.findElements(By.xpath("//*[starts-with(@id,'node-')]")).size();
for (int i = 0; i < numberOfNodes; i++) {
WebElement node = driver.findElement(By.xpath("//*[@id='node-" + Integer.toString(i) + "']"));
// do your logic here
}https://stackoverflow.com/questions/57222398
复制相似问题