Skip to content
Advertisement

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:

wait.until(ExpectedConditions.invisibilityOfElementLocated(pleaseWait));

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:

wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector('loading-content')));

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?

Advertisement

Answer

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:

wait.until(ExpectedConditions.invisibilityOfAllElements(Arrays.asList(pleaseWait)));

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:

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

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

if(pleaseWait.size()==0){
     System.out.println("Element is not visible on the page");
     // Add the further code here
}

And this would not give NoSuchElement exception as well.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement