WebDriver - wait for the element to be used with Java

I am looking for something similar to waitForElementPresent to check if an item is displayed before clicking on it. I thought this could be done using implicitWait , so I used the following:

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

and then click

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

Unfortunately, sometimes he waits for an element, and sometimes not. I searched 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 he waited all, but before the timing he had to wait 10 times 5, 50 seconds. Too much. So I set the implicit wait to 1 sec, and everything still seemed perfect. Because now some things wait 10 seconds before the timeout, but some other timeout after 1 s.

How do you cover the expectation of the presence / visibility of an element in your code? Any hint is noticeable.

+50
java selenium-webdriver webdriver
Jul 31 '12 at 8:28
source share
4 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:

+96
Jul 31 '12 at 10:55
source share

You can use Explicit Expectation or Free Expectation

An Explicit Expectation Example -

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

An example of free time is

 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")); } }); 

More on this TUTORIAL .

+3
Oct 25 '16 at 13:02
source share

We have many race conditions with elementToBeClickable . See https://github.com/angular/protractor/issues/2313 . Something in these lines worked quite 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) ); 
+1
Jul 24 '17 at 7:20
source share

The above expression of expectation is a good example of Explicit Expectation.

Explicit expectations are intellectual expectations that are limited to a specific web element (as indicated above in the x-path).

Using explicit expectations, you basically tell WebDriver at maximum to wait for X units (no matter what you specify as timeoutInSeconds) of time before it refuses.

-one
Oct. 27 '14 at 4:14
source share



All Articles