-1

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;
ARJUN U
  • 63
  • 1
  • 10
  • We can't be sure why it's returning 0 elements without a link to the page. It's possible that ID is not present, is not visible, or there is a delay in page loading that is causing it to return 0. As for why FindAll returns 2, I'm not sure but I would guess that it's executing two searches, each of which returns one element. The search isn't smart enough to realize that they both are the same element. Why would you do these two searches anyway? They are going to return the same thing. – JeffC Sep 19 '17 at 17:23

1 Answers1

1

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.

SiKing
  • 10,003
  • 10
  • 39
  • 90
  • Thank you @SiKing . Can you please shed some light on findAll method as well. – ARJUN U Sep 20 '17 at 05:52
  • @ARJUNU `@FindAll` works like an OR condition: http://seleniumhq.github.io/selenium/docs/api/java/index.html?org/openqa/selenium/support/FindAll.html – SiKing Sep 20 '17 at 15:40