0

I have an automation framework that is driven using page object model using page factory

@FindBy(xpath = "xpathValue")
private WebElement notificationIcon;

I want to have an reusable utility to find all the Webelements. For that I traditionally use findElements methods which takes By parameter. How can I achieve the same using page factory?

I know that I can use the below approach, but I do not want to write for all webelements like this.

@FindBy(xpath = "xpathValue")
private List<WebElement> notificationIcon;

Hence, please find me a solution for this to find all the WebElements using a WebElement type parameter?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
NoNoNo
  • 85
  • 8

1 Answers1

0

The replacement for findElements() in Page Factory can be either of the following:

  • Using FindBy:

    @FindBy(xpath = "xpathValue") 
    private List<WebElement> notificationIcon;
    
  • Using how:

    @FindBy(how = How.XPATH, using = "xpathValue") 
    private List<WebElement> notificationIcon;
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • As I said in question, I do not want this approach, Since I have n number of webelements in n pages, It is taking more time to write same webelement two times, one for findElement and one for findElements. Let me if there is any possibility using WebElement parameter. – NoNoNo Aug 14 '23 at 13:39
  • @NoNoNo You don't need to write same webelement two times. That why [_Page Factory_](https://stackoverflow.com/a/46337213/7429447). You simply need a getter. – undetected Selenium Aug 14 '23 at 13:43
  • @FindBy(xpath = "xpathValue") private WebElement notificationIcon; I do not understand how we can write a getter for List for this? – NoNoNo Aug 14 '23 at 13:46
  • @NoNoNo 1) You can't find all the WebElements withreturn type WebElement. 2) You will need a customized getter as the elements won't be readily available and you need a waiter. – undetected Selenium Aug 14 '23 at 13:50