How to decrease the wait time for NoSuchElementException in Selenium?

In some cases, i know element will not be displayed. but its waiting ~30 Secs.

How to decrease wait time for NoSuchElementException in selenium?

Sample code:

String name; try { name = driver.findElement(By.xpath("XPath")).getText(); } catch (NoSuchElementException e) { name = "Name not displayed"; }
4

3 Answers

I think you're looking for setting the implitic wait time for your driver:

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

For simple cases thats ok to use, for more advanced automation, I'd change it to an explicit wait (using WebDriverWait).

More on waits:

Use WebDriverWait to decrease waiting time ex (wait 5 seconds):

(new WebDriverWait(driver, 5)).until(ExpectedConditions.visibilityOf(name));

We can use explicit wait for this scenario but have to be careful with the expected conditions being used.

WebDriverWait wait=new WebDriverWait(driver,10);
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));

Sometimes visibilityOf(Name) will not work since mostly finding of webelement name needs the findElement statement to be used.

WebElement name=driver.findElement(Locator);

This step may fail if element is not present!

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like