4

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?

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
aeinstein83
  • 289
  • 3
  • 10
  • 22
  • See also http://stackoverflow.com/questions/8008138/selenium-webdriver-and-pagefactory-initialize-listwebelement-elements. – alecxe Jan 30 '15 at 23:11

4 Answers4

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

this works. there was bug in my code.

aeinstein83
  • 289
  • 3
  • 10
  • 22
1

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;
Vivek Singh
  • 3,641
  • 2
  • 22
  • 27
0

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.

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
user1019163
  • 45
  • 1
  • 2
0

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.

Shoaib Mal
  • 89
  • 1
  • 7