Java selenium 如何在 TimeOutException 后重新加载网页?

标签 java google-chrome selenium web

driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS); //Timeout after 10 seconds

我正在使用 ChromeDriver。这是我用来设置超时的代码。

try {
    System.out.println("Selenium connected to " + haruhi.link);
    driver.navigate().to(haruhi.link);  //URL to connect
} catch (UnhandledAlertException e) {
    driver.switchTo().alert().accept();
} catch (TimeoutException e) {
    System.out.println("Timeout, Skip this page");
   skip = true;
} catch (NoSuchWindowException e) {
    skip = true;
}
 if (!skip) {
...
}

这是连接到 URL 的代码。

此代码位于一个 for 循环内,该循环迭代 URL 字符串列表。 一旦“skip”设置为 true,它不会对该网页执行任何操作,而是在循环中的下一次迭代中连接到下一个 URL。

我想做的是当网页挂起时(卡在无限加载中), 我想捕获 TimeoutException,跳过该网页并连接到循环中的下一个 URL。

enter image description here

但是一旦捕获了 TimeoutException,代码就会连接到下一个 URL,但 Chrome 不会收到该命令。 打印出来

“Selenium 连接到 http://gall.dcinside.com/board/comment_view/?id=comic_new1&no=5518557&page=1

但是 chrome URL 仍然显示

"http://gall.dcinside.com/board/comment_view/?id=comic_new1&no=5518554&page=1 "

意思是driver.get();没有工作。

如何正确使用TimeoutException?

最佳答案

尝试使用以下代码,

driver.navigate().refresh();

关于Java selenium 如何在 TimeOutException 后重新加载网页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51394516/

相关文章:

html - 使用xpath的Selenium-如何使用同级元素文本获取元素文本

selenium - Protractor 中的 "defaultTimeoutInterval"何时重置?

java - 在Android中获取当月的最后一个时间戳

java - 集合排序 - 找不到符号

java - 搜索文件 : Beginner Code

javascript - 如何在 JavaScript 中访问 Chrome 拼写检查建议

html - 有没有人在 chrome 中使用背景 gif 看到过这个错误?

javascript - 谷歌浏览器新内容行为

java - 为什么当我移动 @SpringBootApplication 时 Spring Autowire 会失败?

python - 禁用 firefox 另存为 dialog-selenium