Reputation: 501
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";
}
Upvotes: 4
Views: 3144
Reputation: 221
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!
Upvotes: 0
Reputation: 2486
Use WebDriverWait
to decrease waiting time ex (wait 5 seconds):
(new WebDriverWait(driver, 5)).until(ExpectedConditions.visibilityOf(name));
Upvotes: 1
Reputation: 2145
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
Upvotes: 2