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

先知大卫

我有一个包含不同“行”的图像库,其中包含特定数量的“列”(图像)。我想创建一个方法,可以在其中基于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']”的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>
....
马蒂亚斯·穆勒(MathiasMüller)

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

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

我会误解此功能吗?

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

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

来自分类Dev

Selenium Webdriver在Webelement列表中存储Webelement

来自分类Dev

Selenium webdriver store webelement in webelement list

来自分类Dev

Selenium WebDriver Python,搜索WebElement

来自分类Dev

Selenium WebDriver:List <WebElement>中每个WebElement中的findElement()始终返回第一个元素的内容

来自分类Dev

使用Selenium Webdriver设置WebElement的div值

来自分类Dev

Retrieving the value from a Selenium Webdriver WebElement field and passing it to a java variable

来自分类Dev

如何使用Java处理Selenium Webdriver中的隐藏Webelement

来自分类Dev

我如何使用Selenium Webdriver访问XPath更改的WebElement

来自分类Dev

如何在Python中使用Selenium Webdriver提取Webelement

来自分类Dev

Python Selenium WebElement元素-无效的语法

来自分类Dev

Python Selenium WebElement元素-无效的语法

来自分类Dev

Selenium WebDriver元素遍历

来自分类Dev

替代webdriver的ContextClick(webelement)?

来自分类Dev

从Selenium Webdriver WebElement字段中检索值并将其传递给Java变量

来自分类Dev

从Selenium WebDriver中的WebElement获取CSS选择器字符串-Java

来自分类Dev

如何在Selenium Webdriver中使用WebElement获取节点h2的HTML Source?

来自分类Dev

从Selenium WebDriver中的WebElement获取CSS选择器字符串-Java

来自分类Dev

如何使用 selenium webdriver 将 HashSet 和 LinkedHashSet 与 List<WebElement> 结合使用

来自分类Dev

明确等待Selenium Webdriver中的findElements

来自分类Dev

Selenium WebDriver等到元素显示

来自分类Dev

Selenium WebDriver找不到元素

来自分类Dev

Selenium Webdriver帮助查找元素

来自分类Dev

Selenium WebDriver无法获取元素

来自分类Dev

Selenium Webdriver,无法选择元素

来自分类Dev

在Selenium WebDriver中禁用元素

来自分类Dev

Selenium webdriver 等待元素并单击

来自分类Dev

Selenium Webdriver 获取元素(Python)

来自分类Dev

循环 Selenium WebElement

Related 相关文章

  1. 1

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

  2. 2

    Selenium Webdriver在Webelement列表中存储Webelement

  3. 3

    Selenium webdriver store webelement in webelement list

  4. 4

    Selenium WebDriver Python,搜索WebElement

  5. 5

    Selenium WebDriver:List <WebElement>中每个WebElement中的findElement()始终返回第一个元素的内容

  6. 6

    使用Selenium Webdriver设置WebElement的div值

  7. 7

    Retrieving the value from a Selenium Webdriver WebElement field and passing it to a java variable

  8. 8

    如何使用Java处理Selenium Webdriver中的隐藏Webelement

  9. 9

    我如何使用Selenium Webdriver访问XPath更改的WebElement

  10. 10

    如何在Python中使用Selenium Webdriver提取Webelement

  11. 11

    Python Selenium WebElement元素-无效的语法

  12. 12

    Python Selenium WebElement元素-无效的语法

  13. 13

    Selenium WebDriver元素遍历

  14. 14

    替代webdriver的ContextClick(webelement)?

  15. 15

    从Selenium Webdriver WebElement字段中检索值并将其传递给Java变量

  16. 16

    从Selenium WebDriver中的WebElement获取CSS选择器字符串-Java

  17. 17

    如何在Selenium Webdriver中使用WebElement获取节点h2的HTML Source?

  18. 18

    从Selenium WebDriver中的WebElement获取CSS选择器字符串-Java

  19. 19

    如何使用 selenium webdriver 将 HashSet 和 LinkedHashSet 与 List<WebElement> 结合使用

  20. 20

    明确等待Selenium Webdriver中的findElements

  21. 21

    Selenium WebDriver等到元素显示

  22. 22

    Selenium WebDriver找不到元素

  23. 23

    Selenium Webdriver帮助查找元素

  24. 24

    Selenium WebDriver无法获取元素

  25. 25

    Selenium Webdriver,无法选择元素

  26. 26

    在Selenium WebDriver中禁用元素

  27. 27

    Selenium webdriver 等待元素并单击

  28. 28

    Selenium Webdriver 获取元素(Python)

  29. 29

    循环 Selenium WebElement

热门标签

归档