Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wait until an element is present in Selenium?

I'm trying to make Selenium wait for an element that is dynamically added to the DOM after page load. Tried this:

fluentWait.until(ExpectedConditions.presenceOfElement(By.id("elementId")); 

In case it helps, here is fluentWait:

FluentWait fluentWait = new FluentWait<>(webDriver) {     .withTimeout(30, TimeUnit.SECONDS)     .pollingEvery(200, TimeUnit.MILLISECONDS); } 

But it throws a NoSuchElementException - looks like presenceOfElement expects the element to be there so this is flawed. This must be bread and butter to Selenium and don't want to reinvent the wheel... could anyone suggest an alternative, ideally without rolling my own Predicate?

like image 364
Steve Chambers Avatar asked Jan 03 '14 12:01

Steve Chambers


People also ask

How do you get Selenium to wait until the element is present?

We can wait until an element is present in Selenium webdriver. This can be done with the help of synchronization concept. We have an explicit wait condition where we can pause or wait for an element before proceeding to the next step. The explicit wait waits for a specific amount of time before throwing an exception.

How do I wait for an element to appear in Selenium Python?

To wait until element is present, visible and interactable with Python Selenium, we can use the wait. until method. Then we call wait.

How do you wait until an element is clickable on a website?

Wait until the element's . Displayed property is true (which is essentially what visibilityOfElementLocated is checking for). Wait until the element's . Enabled property is true (which is essentially what the elementToBeClickable is checking for).

What is the wait command in Selenium?

The Explicit Wait in Selenium is used to tell the Web Driver to wait for certain conditions (Expected Conditions) or maximum time exceeded before throwing “ElementNotVisibleException” exception. It is an intelligent kind of wait, but it can be applied only for specified elements.


2 Answers

You need to call ignoring with exception to ignore while the WebDriver will wait.

FluentWait<WebDriver> fluentWait = new FluentWait<>(driver)         .withTimeout(30, TimeUnit.SECONDS)         .pollingEvery(200, TimeUnit.MILLISECONDS)         .ignoring(NoSuchElementException.class); 

See the documentation of FluentWait for more info. But beware that this condition is already implemented in ExpectedConditions so you should use

WebElement element = (new WebDriverWait(driver, 10))    .until(ExpectedConditions.elementToBeClickable(By.id("someid"))); 

*Update for newer versions of Selenium:

withTimeout(long, TimeUnit) has become withTimeout(Duration) pollingEvery(long, TimeUnit) has become pollingEvery(Duration) 

So the code will look as such:

FluentWait<WebDriver> fluentWait = new FluentWait<>(driver)         .withTimeout(Duration.ofSeconds(30)         .pollingEvery(Duration.ofMillis(200)         .ignoring(NoSuchElementException.class); 

Basic tutorial for waiting can be found here.

like image 80
Petr Mensik Avatar answered Oct 12 '22 13:10

Petr Mensik


WebDriverWait wait = new WebDriverWait(driver,5) wait.until(ExpectedConditions.visibilityOf(element)); 

you can use this as some time before loading whole page code gets executed and throws and error. time is in second

like image 37
bhupendra Avatar answered Oct 12 '22 13:10

bhupendra