Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebDriver - wait for element using Java

I'm looking for something similar to waitForElementPresent to check whether element is displayed before I click it. I thought this can be done by implicitWait, so I used the following:

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

and then click by

driver.findElement(By.id(prop.getProperty(vName))).click();

Unfortunately, sometimes it waits for the element and sometimes not. I looked for a while and found this solution :

for (int second = 0;; second++) {
    Thread.sleep(sleepTime);
    if (second >= 10)
        fail("timeout : " + vName);
    try {
        if (driver.findElement(By.id(prop.getProperty(vName))).isDisplayed())
            break;
    } catch (Exception e) {
        writeToExcel("data.xls", e.toString(), parameters.currentTestRow, 46);
    }
}
driver.findElement(By.id(prop.getProperty(vName))).click();

And it waited all right, but before timing out it had to wait 10 times 5, 50 seconds. A bit much. So I set the implicitly wait to 1sec and all seemed fine until now. Because now some things wait 10s before timeout but some other things time out after 1s.

How do you cover the waiting for element present/visible in your code? Any hint is appreciable.

like image 945
tom Avatar asked Jul 31 '12 08:07

tom


People also ask

How do you wait for a specific element in Selenium?

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.

What is WebDriver wait in Selenium?

Selenium WebDriverWait is one of the Explicit waits. Explicit waits are confined to a particular web element. Explicit Wait is code you define to wait for a certain condition to occur before proceeding further in the code. Explicit wait is of two types: WebDriverWait.

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).

How do you add an explicit wait?

In order to declare explicit wait, one has to use “ExpectedConditions”. The following Expected Conditions can be used in Explicit Wait. To use Explicit Wait in test scripts, import the following packages into the script. Then, Initialize A Wait Object using WebDriverWait Class.


Video Answer


3 Answers

This is how I do it in my code.

WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>)); 

or

wait.until(ExpectedConditions.elementToBeClickable(By.id<locator>)); 

to be precise.

See also:

  • org.openqa.selenium.support.ui.ExpectedConditions for similar shortcuts for various wait scenarios.
  • org.openqa.selenium.support.ui.WebDriverWait for its various constructors.
like image 138
Ashwin Prabhu Avatar answered Oct 09 '22 13:10

Ashwin Prabhu


You can use Explicit wait or Fluent Wait

Example of Explicit Wait -

WebDriverWait wait = new WebDriverWait(WebDriverRefrence,20);
WebElement aboutMe;
aboutMe= wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("about_me")));     

Example of Fluent Wait -

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)                            
.withTimeout(20, TimeUnit.SECONDS)          
.pollingEvery(5, TimeUnit.SECONDS)          
.ignoring(NoSuchElementException.class);    

  WebElement aboutMe= wait.until(new Function<WebDriver, WebElement>() {       
public WebElement apply(WebDriver driver) { 
return driver.findElement(By.id("about_me"));     
 }  
});  

Check this TUTORIAL for more details.

like image 39
anuja jain Avatar answered Oct 09 '22 12:10

anuja jain


We're having a lot of race conditions with elementToBeClickable. See https://github.com/angular/protractor/issues/2313. Something along these lines worked reasonably well even if a little brute force

Awaitility.await()
        .atMost(timeout)
        .ignoreException(NoSuchElementException.class)
        .ignoreExceptionsMatching(
            Matchers.allOf(
                Matchers.instanceOf(WebDriverException.class),
                Matchers.hasProperty(
                    "message",
                    Matchers.containsString("is not clickable at point")
                )
            )
        ).until(
            () -> {
                this.driver.findElement(locator).click();
                return true;
            },
            Matchers.is(true)
        );
like image 38
andrej Avatar answered Oct 09 '22 12:10

andrej