WebDriver Selenium API: ElementNotFoundErrorException when an element is explicitly present!

sometimes when running tests in WebDriver with Javascript disabled, WebDriver crashes due to an ElementNotFound error when an element is detected and tries to click it.

However, the item is clearly there!

After reading this: http://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions#Q:_My_XPath_finds_elements_in_one_browser,_but_not_in_others._Wh

I came to the conclusion that webdriver should not wait for the web page to load. How to use Webdriver Wait class? Can someone give an example?

+9
webdriver
Nov 06 '10 at 6:24
source share
3 answers

This example was published on Google Groups . According to Google developers:

1 Use implicit expectations. Here, the driver will wait until the appointed timeout until the item is found. Be sure to read javadoc for caution. Using:

driver.get("http://www.google.com"); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); WebElement element = driver.findElement(By.name("q")); driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); // continue with test... 

2 Use the org.openqa.selenium.support.ui.WebDriverWait class. This will be until the expected condition is true, returning this condition (if it is looking for an element). This is much more flexible than implicit waiting, since you can define any custom behavior. Using:

 Function<WebDriver, WebElement> presenceOfElementLocated(final By locator) { return new Function<WebDriver, WebElement>() { public WebElement apply(WebDriver driver) { return driver.findElement(locator); } }; } // ... driver.get("http://www.google.com"); WebDriverWait wait = new WebDriverWait(driver, /*seconds=*/3); WebElement element = wait.until(presenceOfElementLocated(By.name("q")); 
+18
Nov 10 '10 at
source share

Taking nilesh, take it a step further, you can also enable finely tuned search queries (for example, in the context of WebElement) using the SearchContext interface:

 Function<SearchContext, WebElement> elementLocated(final By by) { return new Function<SearchContext, WebElement>() { public WebElement apply(SearchContext context) { return context.findElement(by); } }; } 

Execution is performed using the FluentWait <SearchContext> instance (instead of WebDriverWait). Provide yourself with a good programming interface, wrapping up its execution and the necessary exception handling in the utility (the root of your PageObject type hierarchy is a good place):

 /** * @return The element if found before timeout, otherwise null */ protected WebElement findElement(SearchContext context, By by, long timeoutSeconds, long sleepMilliseconds) { @SuppressWarnings("unchecked") FluentWait<SearchContext> wait = new FluentWait<SearchContext>(context) .withTimeout(timeoutSeconds, TimeUnit.SECONDS) .pollingEvery(sleepMilliseconds, TimeUnit.MILLISECONDS) .ignoring(NotFoundException.class); WebElement element = null; try { element = wait.until(elementLocated(by)); } catch (TimeoutException te) { element = null; } return element; } /** * overloaded with defaults for convenience */ protected WebElement findElement(SearchContext context, By by) { return findElement(context, by, DEFAULT_TIMEOUT, DEFAULT_POLL_SLEEP); } static long DEFAULT_TIMEOUT = 3; // seconds static long DEFAULT_POLL_SLEEP = 500; // milliseconds 

Usage example:

 WebElement div = this.findElement(driver, By.id("resultsContainer")); if (div != null) { asyncSubmit.click(); WebElement results = this.findElement(div, By.id("results"), 30, 500); if (results == null) { // handle timeout } } 
+3
Jun 30 2018-11-11T00:
source share

Free Standby - The best approach, which is the most flexible and customizable "on the fly" (option to ignore exceptions, poll each, timeout):

 public Wait<WebDriver> getFluentWait() { return new FluentWait<>(this.driver) .withTimeout(driverTimeoutSeconds, TimeUnit.SECONDS) .pollingEvery(500, TimeUnit.MILLISECONDS) .ignoring(StaleElementReferenceException.class) .ignoring(NoSuchElementException.class) .ignoring(ElementNotVisibleException.class) } 

Use like this:

 WebElement webElement = getFluentWait().until(x -> { return driver.findElement(elementBy); } ); 

Explicit expected . Well, it's the same as FluentWait , but with pre-configured pollingEvery and a Wait type, for example. FluentWait<WebDriver> (faster to use):

 WebDriverWait wait = new WebDriverWait(driver, 30000); WebElement item = wait.until(ExpectedConditions.visibilityOfElementLocated(yourBy)); 

ImplicitWait . Not recommended as it is configured once for the entire session. This is also used for every find element and expects only presence (no ExpectedConditions , etc.):

 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
0
Nov 20 '16 at 15:40
source share



All Articles