Java selenium - 如何在 TimeoutException 之后正确刷新网页?

2024-05-13

     ChromeOptions options = new ChromeOptions();
       options.addExtensions(new File("extension_6_2_5_0.crx")); //ZenMate
       options.addExtensions(new File("extension_2_9_2_0.crx")); //AdGuard
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(ChromeOptions.CAPABILITY, options);
        driver = new ChromeDriver(options);
          driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS); //Timeout after 10 seconds

这就是我设置 Chrome 驱动程序的方式

   try {
        driver.navigate().to("http://dic.naver.com/");
    }catch (TimeoutException e){
        System.out.println("Time out Exception");
        driver.navigate().refresh();
    }

这是我的代码。

我运行它,然后它按预期捕获 TimeoutException, 但随后浏览器停止执行任何 get() 或 navigator().to() 或刷新命令。

Starting ChromeDriver 2.39.562718 (9a2698cba08cf5a471a29d30c8b3e12becabb0e9) on port 6823
Only local connections are allowed.
Jul 18, 2018 8:47:45 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
[1531936076.034][SEVERE]: Timed out receiving message from renderer: 4.660
[1531936076.633][SEVERE]: Timed out receiving message from renderer: -0.606
Time out Exception
[1531936090.525][SEVERE]: Timed out receiving message from renderer: 10.000
[1531936090.563][SEVERE]: Timed out receiving message from renderer: -0.057
Exception in thread "main" org.openqa.selenium.TimeoutException: timeout
  (Session info: chrome=67.0.3396.99)
  (Driver info: chromedriver=2.39.562718 (9a2698cba08cf5a471a29d30c8b3e12becabb0e9),platform=Windows NT 10.0.17134 x86_64) .remote.RemoteWebDriver$RemoteNavigation.refresh(RemoteWebDriver.java:856)
    at Markov.Bots.Bot_Kancolle.stay(Bot_Kancolle.java:43)
    at Markov.Scroller.main(Scroller.java:71)

相反,它在执行 driver.navigate().refresh(); 时抛出另一个 TimeoutException;在第 43 行

当我执行另一个 driver.get() 时,浏览器只是在 TimeoutException 处停止,不刷新,不连接到新 URL

如何正确捕获 TimeoutException 并重用此浏览器进行下一个 URL 连接???


您可以像这样重用您的会话:

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.*;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.*;
import java.awt.*;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.remote.*;
import org.openqa.selenium.remote.http.W3CHttpCommandCodec;
import org.openqa.selenium.remote.http.W3CHttpResponseCodec;

public class Test {
  public static void main(String[] args) throws InterruptedException, AWTException, IOException {
    RemoteWebDriver driver = new ChromeDriver();
    HttpCommandExecutor executor = (HttpCommandExecutor) driver.getCommandExecutor();
    URL url = executor.getAddressOfRemoteServer();
    SessionId session_id = driver.getSessionId();
    driver.manage().timeouts().pageLoadTimeout(0, TimeUnit.SECONDS);
    try {
      driver.navigate().to("http://dic.naver.com/");
    }catch (TimeoutException e){
      System.out.println("Time out Exception");
      driver = createDriverFromSession(session_id, url);
      driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
      driver.get("https://stackoverflow.com/");
    }
  }

  public static RemoteWebDriver createDriverFromSession(final SessionId sessionId, URL command_executor){
    CommandExecutor executor = new HttpCommandExecutor(command_executor) {

      @Override
      public Response execute(Command command) throws IOException {
        Response response = null;
        if (command.getName() == "newSession") {
          response = new Response();
          response.setSessionId(sessionId.toString());
          response.setStatus(0);
          response.setValue(Collections.<String, String>emptyMap());

          try {
            Field commandCodec = null;
            commandCodec = this.getClass().getSuperclass().getDeclaredField("commandCodec");
            commandCodec.setAccessible(true);
            commandCodec.set(this, new W3CHttpCommandCodec());

            Field responseCodec = null;
            responseCodec = this.getClass().getSuperclass().getDeclaredField("responseCodec");
            responseCodec.setAccessible(true);
            responseCodec.set(this, new W3CHttpResponseCodec());
          } catch (NoSuchFieldException e) {
            e.printStackTrace();
          } catch (IllegalAccessException e) {
            e.printStackTrace();
          }

        } else {
          response = super.execute(command);
        }
        return response;
      }
    };

    return new RemoteWebDriver(executor, new DesiredCapabilities());
  }
}

这是一个工作示例,它创建了一个driver实例以及当driver抛出 TimeoutException,使用相同的会话创建一个新实例(驱动程序)。和driver从一开始就管理窗口driver.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Java selenium - 如何在 TimeoutException 之后正确刷新网页? 的相关文章

随机推荐