How to decrease the wait time for NoSuchElementException in Selenium?

北城以北 提交于 2019-12-10 18:16:29

问题


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";
        }

回答1:


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




回答2:


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

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



回答3:


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!



来源:https://stackoverflow.com/questions/40218019/how-to-decrease-the-wait-time-for-nosuchelementexception-in-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!