如何循环遍历 xpath 表达式? [英] How to loop through xpath expression?
问题描述
我有一个 xpath 表达式:
I have a xpath expression:
referenceIn3DPage=Driver.driver.findElement(By.xpath("//div[3][@class='some-class']/div[1]//label")).getAttribute("id");
我想为 div[]
从 1
循环到 9
.
I want to loop from 1
to 9
for div[]
.
下面是我将 div[i]
从 1 到 9
循环的代码:
Below is my code to loop div[i]
from 1 to 9
:
String referenceIn3DPage =null;
int count=Driver.driver.findElements(By.xpath("//div[3][@class='some-class']//input")).size();
System.out.println("the count="+count);
for(int i=1;i<=count;i++)
{
referenceIn3DPage=Driver.driver.findElement(By.xpath("//div[3][@class='some-class']/div["+i+"]//label/input")).getAttribute("id");
System.out.println("the value in 3d= "+referenceIn3DPage);
}
但是,它不符合我的要求.我想要脚本显示 OSP102 和 8 个更多的参考 ID(请参阅下面给出的链接以获取参考 ID 列表.)
But, it is not working as per my requirement. I want script to display OSP102 and 8 more reference ID (please refer to link given below for list of reference IDs.)
输出:
the count=9
the value in 3d= OSCP102
注意:为了更清楚,请参考我的原始问题.
NOTE: Please refers to my original question for better clarity.
如何抓取对应的文本selenium webdriver 中的复选框?
推荐答案
您可以通过使用 findElements()
并循环遍历前 9 个 div
元素来简化代码您的 xpath 将获取.只需确保您的 xpath 正确即可.
You can simplify your code by using findElements()
and looping through the first 9 div
elements which your xpath will fetch. Just make sure your xpath is correct.
referenceIn3DPage=Driver.driver.findElements(By.xpath("//div[3][@class='some-class']/div"));
for(WebElement e : referenceIn3DPage.sublist(0,9)) {
String idVal = e.findElement(By.ByTagName("label").getAttribute("id"));
System.out.println(idVal);
}
这篇关于如何循环遍历 xpath 表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!