【问题标题】:Understanding WebElement.findElement() and XPATH了解 WebElement.findElement() 和 XPATH
【发布时间】:2015-08-13 01:10:40
【问题描述】:

我想使用WebElement.findElement() API 使用 XPATH //span[@class='child-class'] 在父节点内定位节点。我认为这会将父级内部的<div> 返回给我。但是,它返回给我的是它在整个 DOM 树中找到的第一个。我是否使用了错误的 XPATH?

我也尝试使用.//span[@class='child-class'] 作为 XPATH,但这确实会返回任何内容。

谢谢。

更新:

鉴于下面的 HTML,我想为子标题 <span> 和子日期 <span> 定义一个定位器,并使用 WebElement.findElement() API 定位它们,而不管父级是 "//a/li [1]" 或 "//a/li[2]"

<a>
    <li> parent 1
        <div>
            <span class="child-title child-style">title 1</span>
            <span class="child-date child-style"> date 1</span>
            <span class="child-author">author 1</span>
        </div>
    </li>
</a>
<a>
    <li> parent 2
        <div>
            <span class="child-title child-style">title 2</span>
            <span class="child-date child-style"> date 2</span>
            <span class="child-author">author 3</span>
        </div>
    </li>
</a>
<a>
    <li> parent 3
        <div>
            <span class="child-title child-style">title 3</span>
            <span class="child-date child-style"> date 3</span>
            <span class="child-author">author 3</span>
        </div>
    </li>
</a>

我有一个使用"//a/li[2]" 初始化和定位的WebElement parent2

WebElement child = parent2.findElement(By.xpath("//span[@class='child-author']")); 会给我“作者 1”

WebElement child = parent2.findElement(By.xpath("span[@class='child-author']")); 会给我NoSuchElementException

【问题讨论】:

  • 好的,经过你的编辑,这个问题是无稽之谈!您在问题中引用的大多数 XPath 都不存在于您提供的示例 HTML 中。任何地方都没有//span[@class='child-class'],任何地方都没有//a/li[2]。这实际上可以解释NoSuchElementException

标签: selenium xpath


【解决方案1】:

你的示例代码中有我的 2 个 cmets

1 - 使用您发布的 HTML,找不到 xpath //a/li[2](我们只有 3 个带有 //a/li[1] 的元素)

2 - 假设我们确实有正确的代码,你需要了解 Xpath 中单斜杠和双斜杠的区别

a/b (single slash): select element that has "tag b" and "stands right after" an element that has "a tag" 

例如:

<a>
    <b>
          <d>
               <c>
               </c>
          </d>
    </b>
</a>

a//b (double slash): select element that has "tag b" and is n-level-child an element that has "a tag"

例如:

<a>
    <c>
          <d>
               <b>
               </b>
          </d>
    </c>
</a>

所以,用你的代码

<a>
<li> parent 1
    <div>
        <span class="child-title child-style">title 1</span>
        <span class="child-date child-style"> date 1</span>
        <span class="child-author">author 1</span>
    </div>
</li>
</a>

如果你想获取日期信息,你应该使用

WebElement parent = driver.findElement(By.xpath("//a/li"));
WebElement date = parent.findElement(By.xpath("div/span[contains(@class, 'child-date')]"));
WebElement date = parent.findElement(By.xpath("//span[contains(@class, 'child-date')]"));

代码

WebElement date = parent.findElement(By.xpath("span[contains(@class, 'child-date')]"));

会带出 NoSuchElementException 因为在 [li] 标签后面没有 [span] 标签

希望帮助

【讨论】:

    【解决方案2】:

    尝试类似: 在双 slash(//) 之前使用 dot(.)

    它在给定的父元素下寻找子元素。

    【讨论】:

      【解决方案3】:

      全新的问题......全新的答案。 :(

      尝试类似:

      WebElement parent1 = driver.findElement(By.xpath("//a[1]/li"));   // use a[2] for parent2
      WebElement author = parent1.findElement(By.xpath("span[@class='child-author']"));
      WebElement date = parent1.findElement(By.xpath("span[contains(@class, 'child-date')]"));
      WebElement title = parent1.findElement(By.xpath("span[contains(@class, 'child-title')]"));
      

      【讨论】:

      • 我已尝试删除 XPATH 定位器中的“//”,但没有成功。
      【解决方案4】:

      尝试类似:

      //a/li[contains(text(), 'parent 1')]/div
      

      它请求“&lt;li&gt; 内的 &lt;div&gt;,其文本包含‘父 1’,并且在 &lt;a&gt; 内。
      如果你有更多的父母,它可能不起作用,因为它适用于 contains()(这个 xpath 也将选择 &lt;li&gt; parent 10 ... &lt;/li&gt;)。如果“parent x”是&lt;li&gt; 的属性而不是它的文本会更好。

      【讨论】:

        猜你喜欢
        • 2015-01-09
        • 1970-01-01
        • 1970-01-01
        • 2018-06-15
        • 2010-12-07
        • 2012-07-19
        • 2012-08-23
        • 2013-02-20
        相关资源
        最近更新 更多