我有一个包含不同“行”的图像库,其中包含特定数量的“列”(图像)。我想创建一个方法,可以在其中基于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']”的Webelement,而不仅是“ 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']"));
}
我的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>
....
.
在第二个表达式的//
(descendant-or-self::
轴)前面添加:
return sectionGallery.findElements(By.xpath(".//div[@class='gallery-horizontal-row']"));
我会误解此功能吗?
即使您选择了输入的一个子集作为第二个表达式的起点,也不是完全,而是以开头的表达式//
选择了输入文档中任何位置的元素。//
尽可能避免使用它-过度使用的轴-尤其是XPath初学者。
另一方面,以开头的表达式.//
实际上以上下文节点为起点。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句