java WebDriver PageFactory 查找元素列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28243653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:15:44  来源:igfitidea点击:

WebDriver PageFactory Find Elements List

javaselenium-webdriverwebdriverpageobjects

提问by aeinstein83

I have multiple elements on a page and I would like to initialize them using PageFactory.

我在一个页面上有多个元素,我想使用 PageFactory 初始化它们。

I have tried using following

我试过使用以下

@FindBy(xpath = "//*[contains(@class,'x-grid-tree-node-leaf')]")    
    List<WebElement> allElements;

but this returns only one element.

但这仅返回一个元素。

now, if I use the traditional way for finding elements

现在,如果我使用传统的方式来查找元素

List<WebElement> allElements = driver.findElements(By.xpath("//*[contains(@class,'x-grid-tree-node-leaf')]"));

this returns 4 elements

这将返回 4 个元素

any pointers what could be the issue?

任何指针可能是什么问题?

回答by aeinstein83

@FindBy(xpath = "//*[contains(@class,'x-grid-tree-node-leaf')]")    
List<WebElement> allElements;

this works. there was bug in my code.

这有效。我的代码中有错误。

回答by Vivek Singh

Use FindAllannotation to get series of @FindBy tagsand search for all elements that match any of the FindBy criteria.

使用FindAll注释获取一系列 @FindBy 标签并搜索与任何 FindBy 条件匹配的所有元素。

@FindAll(@FindBy(how = How.XPATH, using = "//*[contains(@class,'x-grid-tree-node-leaf')]"))
List<WebElement> allElements;

回答by Shoaib Mal

Instead of using @FindBy annotation, use @FindAllBy annotation.Try this!

不要使用@FindBy 注释,而是使用@FindAllBy 注释。试试这个!

@FindAllBy(xpath = "//*[contains(@class,'x-grid-tree-node-leaf')]")    
List<WebElement> allElements;

Here's the linkfor FindAllBy java class.

这是FindAllBy java 类的链接

回答by user1019163

Have you tried running your xpath in Chrome Developer tool or in Firebug?

您是否尝试过在 Chrome Developer tool 或 Firebug 中运行您的 xpath?

List<WebElement> allElements = driver.findElements(By.xpath("//*[contains(@class,'x-grid-tree-node-leaf')]"));

should work.

应该管用。