Selenium:如何获取给定 xpath 的子 span 元素列表

huangapple go评论113阅读模式
英文:

Selenium: How to get list of child span elements given a xpath

问题

  1. 我想要获取以下 XPath 表达式对应的所有子 span 元素:
  2. //label[@for='someText']/span
  3. 这会返回:
  4. <span class="someClass">"找到这个文本"</span>
  5. <span id="someId">"找到这个文本2"</span>
  6. 基本上,嵌套的 span 元素数量可能会动态变化,在这个情况下我们有 2 span 子元素,有些情况下可能只有 1 个或更多的 span 元素。
  7. 目标:
  8. 这样做的目的是为了获取所有这些元素的文本以进行比较。一旦我获取了这些元素,我可以迭代遍历每一个,并根据它们的属性提取我想要的数据。
  9. 示例 HTML 格式:
  10. <label for="someText">
  11. <span class="nestedClass">
  12. <h2 id="id">
  13. <span>"你好"</span>
  14. </h2>
  15. <span class="someClass">"找到这个文本"</span>
  16. <br>
  17. <span id="someId">"找到这个文本2"</span>
  18. </span>
  19. </label>
  20. 有没有办法实现这个?有人可以帮我实现这个,或者有更好的方法吗?
  21. 谢谢。
英文:

I want to get all child span elements for the following xpath

  1. //label[@for='someText']/span

This should return me

  1. <span class="someClass">"Find this text"</span>
  2. <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:

  1. <label for="someText">
  2. <span class="nestedClass">
  3. <h2 id="id">
  4. <span>"Hello"</span>
  5. </h2>
  6. <span class="someClass">"Find this text"</span>
  7. <br>
  8. <span id="someId">"Find this text2"</span>
  9. </span>
  10. </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应该适用于您。

  1. List<WebElement> elements = driver.findElements(By.xpath("//label[@for='someText']/span/span"));

或者忽略其父级为h2的span标签。

  1. List<WebElement> elements = driver.findElements(By.xpath("//label[@for='someText']/span//span[not(ancestor::h2)]"));
英文:

Use either of the xpath should work for you.

  1. List&lt;WebElement&gt; elements = driver.findElements(By.xpath(&quot;//label[@for=&#39;someText&#39;]/span/span&quot;));

Or ignore span tag who's parents h2

  1. List&lt;WebElement&gt; elements = driver.findElements(By.xpath(&quot;//label[@for=&#39;someText&#39;]/span//span[not(ancestor::h2)]&quot;));

答案2

得分: 0

你可以使用以下的 XPath 表达式:

  1. //label[@for=&quot;someText&quot;]/span/*[not(*)]/text()

这里的谓词 *[not(*)] 会过滤掉叶子节点的 span。

英文:

You could use following XPath:

  1. //label[@for=&quot;someText&quot;]/span/*[not(*)]/text()

Here predicate *[not(*)] will filter leaf span nodes.

答案3

得分: -1

你可以使用 size() 方法循环遍历元素,或者只需使用 for 循环迭代列表。

  1. List<WebElement> mySpans = driver.findElements(By.xpath("//label[@for='someText']/span//span"));
  2. int totalSpans = mySpans.size(); // 这是 span 的总数 - 动态
  3. // 或者只需使用 for 循环迭代 webelement 列表
  4. for (WebElement span : mySpans) {
  5. System.out.print(span.getText());
  6. }
英文:

You can loop through elements using the size() or simply iterate the list using the for loop.

  1. List&lt;WebElement&gt; mySpans = driver.findElements(By.xpath(&quot;//label[@for=&#39;someText&#39;]/span//span&quot;));
  2. int totalSpans = mySpans.size(); // This is the total count of spans - dynamic
  3. // or simply iterate the list of webelements using for loop
  4. for(WebElement span : mySpans)
  5. {
  6. System.out.print(span.getText());
  7. }

huangapple
  • 本文由 发表于 2020年10月15日 01:28:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64358565.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定