在某些情况下,我知道元素不会显示。但等待约 30 秒。
如何减少 selenium 中 NoSuchElementException
的等待时间?
示例代码:
String name;
try {
name = driver.findElement(By.xpath("XPath")).getText();
} catch (NoSuchElementException e) {
name = "Name not displayed";
}
最佳答案
我认为您正在寻找为您的驱动程序设置隐式等待时间:
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
对于可以使用的简单情况,对于更高级的自动化,我会将其更改为显式等待(使用 WebDriverWait
)。
关于等待的更多信息: http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp
关于java - 如何减少 Selenium 中 NoSuchElementException 的等待时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40218019/