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
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With