【问题标题】:Selenium WebDriver- WebElement.FindElements returns more elements then expectedSelenium WebDriver- WebElement.FindElements 返回比预期更多的元素
【发布时间】:2015-05-14 09:11:52
【问题描述】:

我有一个包含不同“行”的图片库,其中包含特定数量的“列”(图片)。我想创建一个方法,我可以在其中根据 x,y 选择特定图像。

所以我使用 pageObjects 搜索包含画廊的部分。

@FindBy(how = How.XPATH, using = "//section[@id='gallery']")
private WebElement sectionGallery;

然后,我创建了一个小方法,它将返回此画廊的所有行

public List<WebElement> getGalleryRows(){
        return sectionGallery.findElements(By.xpath("//div[@class='gallery-horizontal-row']"));
    }

这就是问题所在。我正在获取每个具有 xpath 表达式“//div[@class='gallery-horizo​​ntal-row']”的 Web 元素,而不仅仅是“sectiongallery”Web 元素下的 Web 元素。

我是否误解了此功能? 对于下面的 HTML 源,我希望返回 3 个 Web 元素,但我得到了 4 个。

当我对完整的 Xpath 表达式执行 Driver.FindElements 时,它只返回 3 个元素。

public List<WebElement> getGalleryRows(){
        return DriverManager.getDriver().findElements(By.xpath("//section[@id='gallery']//div[@class='gallery-horizontal-row']"));
    }

我的 HTML 隐藏源代码:

    <section data-section-title="Galerie" id="gallery">
<header>
    <h1>Galerie</h1>
</header>
<div>
    <div >  
        <div class="gallery-horizontal-row" style="height: 220px;">
            <div>
                <article class="gallery-item"></article>
                <article class="gallery-item"></article>
                <article class="gallery-item"></article>
                <article class="gallery-item"></article>
                <article class="gallery-item"></article>
            </div>
        </div>
        <div class="gallery-horizontal-row" style="height: 220px;">
            <div>
                <article class="gallery-item"></article>
                <article class="gallery-item"></article>
                <article class="gallery-item"></article>
            </div>
        </div>
        <div class="gallery-horizontal-row" style="height: 220px;">
            <div>
                <article class="gallery-item"></article>
                <article class="gallery-item"></article>
                <article class="gallery-item"></article>
                <article class="gallery-item"></article>
            </div>
        </div>
    </div>
</div>
</section>
<section id="other">
    ....
</section>  
<section id="other2">
    ....
</section>  
<section id="other3">
    ....
</section>  
<section data-section-title="other4" id="other4">
<header>
    <h1>Other4</h1>
</header>
<div>
    <div >  
        <div class="gallery-horizontal-row" style="height: 220px;">
            <div>
....

【问题讨论】:

    标签: selenium xpath selenium-webdriver


    【解决方案1】:

    在第二个表达式的//descendant-or-self::轴)前面添加.

    return sectionGallery.findElements(By.xpath(".//div[@class='gallery-horizontal-row']"));
    

    我是否误解了这个功能?

    不完全是,以// 开头的表达式会选择输入文档中的anywhere 元素,即使您已选择输入的子集作为第二个表达式的起点。尽可能避免使用// - 这是一个过度使用的轴 - 尤其是 XPath 初学者。

    另一方面,以.// 开头的表达式实际上是以上下文节点为起点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-01
      • 1970-01-01
      • 2019-03-20
      • 2020-12-18
      • 1970-01-01
      • 2018-07-31
      相关资源
      最近更新 更多