Dav*_*idH 3 selenium xpath selenium-webdriver
我有一个包含不同"行"的图库,其中包含特定数量的"列"(图像).我想创建一个方法,我可以根据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-horizontal-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>
<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>
....
Run Code Online (Sandbox Code Playgroud)
.在第二个表达式中添加//(descendant-or-self::轴)前面:
return sectionGallery.findElements(By.xpath(".//div[@class='gallery-horizontal-row']"));
Run Code Online (Sandbox Code Playgroud)
我误解了这个功能吗?
虽然您已选择输入的子集作为第二个表达式的起点,但不是完全,而是以在输入文档中的任何位置//选择元素开始的表达式.尽可能避免- 它是一个严重过度使用的轴 - 特别是XPath初学者.//
另一方面,以.//真正开始的表达式将上下文节点作为起点.
| 归档时间: |
|
| 查看次数: |
2535 次 |
| 最近记录: |