gpt4 book ai didi

java - 使用 for 循环从任意大小的列表中获取多个 WebElement

转载 作者:行者123 更新时间:2023-12-02 09:39:24 24 4
gpt4 key购买 nike

我正在尝试使用长度不固定的 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 中),该循环抓取 xpath 中从“node-1”开始到有多个节点的每个 web 元素,然后将这些元素放入列表中以供以后使用

最佳答案

puts those elements into a list for later use

以下是简单的方法..带有列表供以后使用 -

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 是供以后使用的列表

关于java - 使用 for 循环从任意大小的列表中获取多个 WebElement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57222398/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com