4

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";
        }
Girish Bellamkonda
  • 491
  • 1
  • 8
  • 17
  • Do you have a real example that works just like you described? I'm quite sure that provided code should immediately raise an exception. Described behavior available only if to use `WebdriverWait` + `ExpectedConditions` – Andersson Oct 24 '16 at 12:14
  • Try block is inside the loop of 626 iterations, But expecting only 200+ values to be found and write the name into an excel file for future reference. Have used implicit wait for 30 secs after opening browser. – Girish Bellamkonda Oct 24 '16 at 12:18
  • you have to use explicit waits described in the webdriver documentation http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp – metar Oct 24 '16 at 12:19
  • Remove the implicit wait and use explicit/fluent waits only for the statements required and in general do not mix the two. The delay is caused by the implicit wait. – Sai Oct 24 '16 at 16:48

3 Answers3

2

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: http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp

Len
  • 2,093
  • 4
  • 34
  • 51
1

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

(new WebDriverWait(driver, 5)).until(ExpectedConditions.visibilityOf(name));
Issam El-atif
  • 2,366
  • 2
  • 17
  • 22
0

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!

prithvi394
  • 221
  • 1
  • 6