Java Selenium-如何在TimeoutException之后正确刷新网页? [英] Java selenium - how to properly refresh a webpage after TimeoutException?

查看:138
本文介绍了Java Selenium-如何在TimeoutException之后正确刷新网页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

     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驱动程序的方式

This is how I set my Chrome Driver

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

这是我的代码.

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

I run it, then it catches TimeoutException as it is supposed to, but then the browser stops taking any get() or navigate().to() or refresh commands.

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行

Instead it throws another TimeoutException when it executes driver.navigate().refresh(); at line 43

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

The browser simply stops at TimeoutException, does not refresh, does not connect to new URL when I execute another driver.get()

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

How do I properly catch TimeoutException and reuse this browser for next URL connection???

推荐答案

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

You can reuse your session like this:

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管理窗口.

This is a working example, which creates a driver instance and when the driver throws TimeoutException, creates a new instance(driver) with the same session. And driver manages the window from the first driver.

这篇关于Java Selenium-如何在TimeoutException之后正确刷新网页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆