英文:
Selenium: How to get list of child span elements given a xpath
问题
我想要获取以下 XPath 表达式对应的所有子 span 元素:
//label[@for='someText']/span
这会返回:
<span class="someClass">"找到这个文本"</span>
<span id="someId">"找到这个文本2"</span>
基本上,嵌套的 span 元素数量可能会动态变化,在这个情况下我们有 2 个 span 子元素,有些情况下可能只有 1 个或更多的 span 元素。
目标:
这样做的目的是为了获取所有这些元素的文本以进行比较。一旦我获取了这些元素,我可以迭代遍历每一个,并根据它们的属性提取我想要的数据。
示例 HTML 格式:
<label for="someText">
<span class="nestedClass">
<h2 id="id">
<span>"你好"</span>
</h2>
<span class="someClass">"找到这个文本"</span>
<br>
<span id="someId">"找到这个文本2"</span>
</span>
</label>
有没有办法实现这个?有人可以帮我实现这个,或者有更好的方法吗?
谢谢。
英文:
I want to get all child span elements for the following xpath
//label[@for='someText']/span
This should return me
<span class="someClass">"Find this text"</span>
<span id="someId">"Find this text2"</span>
Basically, the number of nested span elements may change dynamically, in this case we have 2 span child elements, in some cases there can just be 1 or more span elements.
Goal:
The intention behind this is to fetch the text for all those elements for comparison purposes. Once i get those elements, I can iterate through each of them and based on their attributes I can fetch data i want.
Sample HTML format:
<label for="someText">
<span class="nestedClass">
<h2 id="id">
<span>"Hello"</span>
</h2>
<span class="someClass">"Find this text"</span>
<br>
<span id="someId">"Find this text2"</span>
</span>
</label>
Is there a way I can achieve this? Could someone please help me with this or any better approach in doing this?
Thank you.
答案1
得分: 2
使用以下任一xpath
应该适用于您。
List<WebElement> elements = driver.findElements(By.xpath("//label[@for='someText']/span/span"));
或者忽略其父级为h2的span
标签。
List<WebElement> elements = driver.findElements(By.xpath("//label[@for='someText']/span//span[not(ancestor::h2)]"));
英文:
Use either of the xpath
should work for you.
List<WebElement> elements = driver.findElements(By.xpath("//label[@for='someText']/span/span"));
Or ignore span tag who's parents h2
List<WebElement> elements = driver.findElements(By.xpath("//label[@for='someText']/span//span[not(ancestor::h2)]"));
答案2
得分: 0
你可以使用以下的 XPath 表达式:
//label[@for="someText"]/span/*[not(*)]/text()
这里的谓词 *[not(*)]
会过滤掉叶子节点的 span。
英文:
You could use following XPath:
//label[@for="someText"]/span/*[not(*)]/text()
Here predicate *[not(*)]
will filter leaf span nodes.
答案3
得分: -1
你可以使用 size() 方法循环遍历元素,或者只需使用 for 循环迭代列表。
List<WebElement> mySpans = driver.findElements(By.xpath("//label[@for='someText']/span//span"));
int totalSpans = mySpans.size(); // 这是 span 的总数 - 动态
// 或者只需使用 for 循环迭代 webelement 列表
for (WebElement span : mySpans) {
System.out.print(span.getText());
}
英文:
You can loop through elements using the size() or simply iterate the list using the for loop.
List<WebElement> mySpans = driver.findElements(By.xpath("//label[@for='someText']/span//span"));
int totalSpans = mySpans.size(); // This is the total count of spans - dynamic
// or simply iterate the list of webelements using for loop
for(WebElement span : mySpans)
{
System.out.print(span.getText());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论