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.
java selenium-webdriver webdriver
tom Jul 31 '12 at 8:28 2012-07-31 08:28
source share