小编Dav*_*idH的帖子

Selenium WebDriver- WebElement.FindElements返回更多预期的元素

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

所以我使用pageObjects搜索包含图库的部分.

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

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

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

这就是问题所在.我得到的每个Webelement都有xpath表达式"// div [@ class ='gallery-horizo​​ntal-row']",而不仅仅是"sectiongallery"webelement下面的webelements.

我是否误解了这个功能?对于HTML源代码,我希望返回3个Webelements,但是我得到4个.

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

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

我的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> …
Run Code Online (Sandbox Code Playgroud)

selenium xpath selenium-webdriver

3
推荐指数
1
解决办法
2535
查看次数

标签 统计

selenium ×1

selenium-webdriver ×1

xpath ×1