Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium Webdriver "Expectedconditions.not" is not working as expected

WebDriverWait wait = new WebDriverWait(driver, 60)

WebElement element = driver.findElement(By.xpath("//div[contains(text(),'Loading...')]"));
System.out.println("Test");
wait.until(ExpectedConditions.not(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[contains(text(),'Loading...')]"))));
System.out.println("Test");

Trying to wait for the page loading to be completed. The first "test" is printed in the console and below exception is printed on exceuting the wait.until statement. Even after the loading screen is gone the wait.until is still waiting. Already tried Staleness of the element also and does not work, getting the same timeout exception. Once the loading is completed the element is no more available in the DOM

like image 594
Srivardhan Avatar asked Nov 12 '13 13:11

Srivardhan


People also ask

What is selenium Expectedconditions?

Expected Conditions provided by Selenium WebDriver are used for performing Explicit Waits on a certain condition. The Selenium WebDriver waits for the specified condition to occur before it can proceed further with the execution.

How wait until element is not present in selenium?

To check if an element no longer exists on the page, we can take the help of the expected condition invisibilityOfElementLocated. To implement explicit wait conditions, we have to take help of the WebDriverWait and ExpectedCondition class.

How check alert is present or not in selenium?

New Selenium IDE We can check if an alert exists with Selenium webdriver. An alert is created with the help of Javascript. We shall use the explicit wait concept in synchronization to verify the presence of an alert.

Which of the following WebDriver methods counts the number of elements?

The total number of links in a page can be counted with the help of findElements() method. The logic is to return a list of web elements with tagname anchor, then getting the size of that list.


2 Answers

When you want to wait for element to be not present, instead of presenceOfElementLocated use presenceOfAllElementsLocatedBy:

wait.until(ExpectedConditions.not(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//div[contains(text(),'Loading...')]"))));

It will wait until there are no elements on the page that fit the locator.

like image 90
Kirill Litvinenko Avatar answered Oct 16 '22 11:10

Kirill Litvinenko


you are not waiting for the element to be visible in the first statement,i.e,

WebElement element = driver.findElement(By.xpath("//div[contains(text(),'Loading...')]"));

i think this is causing the NoSuchElementException...
you can try the following :

new WebDriverWait(driver,60).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'Loading...')]")));

new WebDriverWait(driver,60).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[contains(text(),'Loading...')]")));

the above code will first wait for the visibility of the element,and then its invisibility.

like image 32
Amith Avatar answered Oct 16 '22 11:10

Amith