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

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

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&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

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

答案2

得分: 0

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

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

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

英文:

You could use following XPath:

//label[@for=&quot;someText&quot;]/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&lt;WebElement&gt; mySpans = driver.findElements(By.xpath(&quot;//label[@for=&#39;someText&#39;]/span//span&quot;));
    	
    	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());
    	}

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:

确定