Reputation: 289
I have multiple elements on a page and I would like to initialize them using 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
any pointers what could be the issue?
Upvotes: 4
Views: 24865
Reputation: 45
Have you tried running your xpath in Chrome Developer tool or in Firebug?
List<WebElement> allElements = driver.findElements(By.xpath("//*[contains(@class,'x-grid-tree-node-leaf')]"));
should work.
Upvotes: 0
Reputation: 289
@FindBy(xpath = "//*[contains(@class,'x-grid-tree-node-leaf')]")
List<WebElement> allElements;
this works. there was bug in my code.
Upvotes: 3
Reputation: 3649
Use FindAll annotation to get series of @FindBy tags and search for all elements that match any of the FindBy criteria.
@FindAll(@FindBy(how = How.XPATH, using = "//*[contains(@class,'x-grid-tree-node-leaf')]"))
List<WebElement> allElements;
Upvotes: 1
Reputation: 89
Instead of using @FindBy annotation, use @FindAllBy annotation.Try this!
@FindAllBy(xpath = "//*[contains(@class,'x-grid-tree-node-leaf')]")
List<WebElement> allElements;
Here's the link for FindAllBy java class.
Upvotes: 0