FindBys and List<WebElement> always return null list

1.3k views Asked by At

As far as my understanding goes, FindBys Annotation in pagefactory returns you elements which satisfies all the condition mentioned inside. The code below always returns 0 elements.

Similarly,If I'm using FindAll annotation with same id and Xpath attribute it is returning me 2 webelements. Can anyone help me in understanding the results.

 @FindBys
 (   
   {
    @FindBy(xpath="//*[@id='ctl00_ctl00_divWelcome']"),
    @FindBy(id="ctl00_ctl00_divWelcome")
    
   }
   )
 public List<WebElement> allElementsInList;

1

There are 1 answers

2
SiKing On BEST ANSWER

Your understanding is wrong.

The documentation for @FindBy says:

Used to mark a field on a Page Object to indicate that lookup should use a series of @FindBy tags in a chain as described in org.openqa.selenium.support.pagefactory.ByChained

Further, the documentation for ByChained says:

Mechanism used to locate elements within a document using a series of other lookups. This class will find all DOM elements that matches each of the locators in sequence, e.g. driver.findElements(new ByChained(by1, by2)) will find all elements that match by2 and appear under an element that matches by1.

So in your example, you are looking for an element by XPath with a specific ID, and then its child element by the same ID ... which, of course, is not going to return anything.