【发布时间】: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-horizontal-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