【问题标题】:Java selenium - how to properly refresh a webpage after TimeoutException?Java selenium - TimeoutException 后如何正确刷新网页?
【发布时间】:2018-07-18 18:03:07
【问题描述】:
     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() 或 navigate().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 连接???

【问题讨论】:

    标签: java google-chrome selenium web


    【解决方案1】:

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

    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开始管理窗口。

    【讨论】:

      【解决方案2】:

      也许您可以在超时异常后使用 javascript 停止页面加载,如下所示。这可能会对您有所帮助。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-12-30
        • 2020-08-25
        • 2020-09-07
        • 2012-03-25
        • 1970-01-01
        • 1970-01-01
        • 2014-05-21
        相关资源
        最近更新 更多