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
WebDriver PageFactory Find Elements List
提问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
回答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.
应该管用。