Skip to content

How to wait for invisibility of an element through PageFactory using Selenium and Java

Is there a way to wait for an element not present in Selenium using PageFactory annotations?

When using:

@FindBy(css= '#loading-content')
WebElement pleaseWait;

to locate the element, and then:


I would get:

org.opeqa.selenium.WebElement cannot be converted to org.openqa.selenium.By

I am able to do what I need by using:


However, I would like to be able to use the PageFactory annotations in order to keep the framework consistent. Is there a way to do this?


invisibilityOfElementLocated is expecting a locator but you are sending a web-element and that is why it is throwing an error. You can perform the operation by checking the webelement list by using:


Updated Answer:
If you want to check that the element is not present on the page then you can check its list size is equal to 0 or not, as its list size will be 0 when its not displayed on the UI.

You can get the list of the element by using:

List<WebElement> pleaseWait;

And you can check the list size equals to 0 by using:

     System.out.println("Element is not visible on the page");
     // Add the further code here

And this would not give NoSuchElement exception as well.