更改 Seleniums 默认的 TimeoutException
Change Seleniums default TimeoutException
在我的测试中,一些通过 css selector
定位的元素需要很长时间才能显示,因为服务器非常慢。当 运行 通过 jenkins 而不是在 eclipse 上进行本地测试时,我经常会遇到此错误。
> org.openqa.selenium.TimeoutException: Expected condition failed:
> waiting for visibility of element located by By.cssSelector:
> #ctl00_ContentPlaceHolder1_uctlSettingUpPaymentCollectionGrid1_gvGroup_ctl02_deleteGroup
> (tried for 10 second(s) with 500 milliseconds interval)
是否可以在 selenium 上将此时间增加到 10 秒以上?
尝试隐式等待 - docs here
There is a second type of wait that is distinct from explicit wait
called implicit wait. By implicitly waiting, WebDriver polls the DOM
for a certain duration when trying to find any element. This can be
useful when certain elements on the webpage are not available
immediately and need some time to load.
你为你的驱动程序设置一次,它是一个动态等待,直到你指定的超时。
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
或者,您可以对每个需要它的元素使用 explicit waits:
new WebDriverWait(driver, Duration.ofSeconds(30)).until(ExpectedConditions.elementToBeClickable(<<Your-Identifier>>));
通过显式等待,您可以确保某些 ExpectedConditions 已准备就绪,并且您可以控制要同步的元素的超时。
Java 的预期条件列表是 here
一般建议您尽可能选择进近方式:
Warning: Do not mix implicit and explicit waits. Doing so can cause
unpredictable wait times. For example, setting an implicit wait of 10
seconds and an explicit wait of 15 seconds could cause a timeout to
occur after 20 seconds.
在我的测试中,一些通过 css selector
定位的元素需要很长时间才能显示,因为服务器非常慢。当 运行 通过 jenkins 而不是在 eclipse 上进行本地测试时,我经常会遇到此错误。
> org.openqa.selenium.TimeoutException: Expected condition failed:
> waiting for visibility of element located by By.cssSelector:
> #ctl00_ContentPlaceHolder1_uctlSettingUpPaymentCollectionGrid1_gvGroup_ctl02_deleteGroup
> (tried for 10 second(s) with 500 milliseconds interval)
是否可以在 selenium 上将此时间增加到 10 秒以上?
尝试隐式等待 - docs here
There is a second type of wait that is distinct from explicit wait called implicit wait. By implicitly waiting, WebDriver polls the DOM for a certain duration when trying to find any element. This can be useful when certain elements on the webpage are not available immediately and need some time to load.
你为你的驱动程序设置一次,它是一个动态等待,直到你指定的超时。
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
或者,您可以对每个需要它的元素使用 explicit waits:
new WebDriverWait(driver, Duration.ofSeconds(30)).until(ExpectedConditions.elementToBeClickable(<<Your-Identifier>>));
通过显式等待,您可以确保某些 ExpectedConditions 已准备就绪,并且您可以控制要同步的元素的超时。
Java 的预期条件列表是 here
一般建议您尽可能选择进近方式:
Warning: Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. For example, setting an implicit wait of 10 seconds and an explicit wait of 15 seconds could cause a timeout to occur after 20 seconds.