了解WebElement.findElement()和XPATH

时间:2014-07-09 18:28:11

标签: selenium xpath

我想使用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>

我使用WebElement parent2初始化"//a/li[2]"

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

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

4 个答案:

答案 0 :(得分:10)

我的示例代码有两条评论

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" 

E.g:

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

E.g:

<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]标记

希望帮助

答案 1 :(得分:7)

尝试以下方法: 在加倍dot(.)

之前使用slash(//)

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

答案 2 :(得分: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')]"));

答案 3 :(得分:1)

尝试类似:

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

请求“<div><li>的{​​{1}}文字中包含”父级1“且位于<a>内。{
如果你有更多的父母,它可能无效,因为它适用于contains()(此xpath也会选择<li> parent 10 ... </li>)。如果“父x”是<li>的属性而不是其文本,那会更好。