如何阻止selenium webdriver等待页面加载?

use*_*879 4 java selenium webdriver pageload

有时,当我进行测试时,网站永远不会停止加载,我的测试就会卡在上面.如何设置driver.get()方法不等待页面加载?如果它不可能有任何工作或其他方法可以取代driver.get()?

Jim*_*ans 11

退出页面加载等待的最简单方法是设置页面加载超时.当超时到期时,您可以捕获TimeoutException并继续下一步测试.调用它的代码如下所示:

// Set the page load timeout to 10 seconds.
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);

try {
  driver.get("http://url/to/my/slow/loading/page");
} catch (TimeoutException e) {
  // Ignore the exception.
}

// Proceed with your next step here.
Run Code Online (Sandbox Code Playgroud)

请注意,您可能必须使用WebDriverWait或类似,以确保页面上存在您感兴趣的元素.