如果网址未打开,如何关闭selenium webdriver

时间:2015-04-11 03:49:37

标签: java selenium

这个问题有很多答案(driver.close()),但没有一个对我有所帮助。 我尝试每分钟使用selenium web驱动程序打开一个网站。如果网站没有在一分钟内打开代码需要关闭驱动程序,但它没有关闭。因为,网页标题显示为连接。请帮助我为此工作。

1 个答案:

答案 0 :(得分:1)

除非您终止浏览器并退出驱动程序实例,否则没有直接的方法可以实现此目的。最简单的方法可能是添加一些硬编码延迟并使用JavaScript杀死会话

driver.get("something");
Thread.sleep(1000);
//Alternative to javaScript is to use Actions Class and send ESC key to stop execution
//Actions actions = new Actions(driver);
//actions.sendKeys(Keys.ESCAPE);

((JavascriptExecutor) driver).executeScript("window.stop;");
driver.quit();

修改

如果你想添加另一种方法来绕过会话的杀戮,如果网站正确加载试试这个

driver.get("");

 try {

     (new WebDriverWait(driver, 1)).until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.id("something we know should exist if page load");

 } catch (Exception ex) {

     //Alternative to javaScript is to use Actions Class and send ESC key to stop execution
     //Actions actions = new Actions(driver);
     //actions.sendKeys(Keys.ESCAPE);
     ((JavascriptExecutor) driver).executeScript("window.stop;");
      driver.quit();
 }