java - Selenium 偶尔出现 UnreachableBrowserException

标签 java selenium exception selenium-webdriver web-scraping

我正在尝试使用 Java 中的 Selenium 访问多个网站。有时,我会收到 UnreachableBrowserException。我已经阅读了许多有关此错误的线程,但似乎有许多不同的原因导致该错误。当我尝试访问新页面时,大约 1% 的情况下会出现此错误,并且我无法找到发生的事件之间的任何相似之处。我目前使用的是 Firefox,但我也尝试过 Internet Explorer 并遇到了类似的错误。我一次只打开一个页面,并尝试使用同一窗口并完全退出驱动程序,然后再尝试访问另一页,但无论哪种方式,错误仍然发生。重要的是要注意,我并不总是遇到此错误,有时我的代码可以运行而不会发生这种情况。这是错误消息:

Jan 12, 2015 10:39:40 PM org.apache.http.impl.execchain.RetryExec execute
INFO: I/O exception (java.net.SocketException) caught when processing request to {}-      http://127.0.0.1:7055: Permission denied: connect
Jan 12, 2015 10:39:40 PM org.apache.http.impl.execchain.RetryExec execute
INFO: Retrying request to {}->http://127.0.0.1:7055
Jan 12, 2015 10:39:40 PM org.apache.http.impl.execchain.RetryExec execute
INFO: I/O exception (java.net.SocketException) caught when processing request to {}->http://127.0.0.1:7055: Permission denied: connect
Jan 12, 2015 10:39:40 PM org.apache.http.impl.execchain.RetryExec execute
INFO: Retrying request to {}->http://127.0.0.1:7055
Jan 12, 2015 10:39:40 PM org.apache.http.impl.execchain.RetryExec execute
INFO: I/O exception (java.net.SocketException) caught when processing request to {}->http://127.0.0.1:7055: Permission denied: connect
Jan 12, 2015 10:39:40 PM org.apache.http.impl.execchain.RetryExec execute
INFO: Retrying request to {}->http://127.0.0.1:7055
Jan 12, 2015 10:39:40 PM org.apache.http.impl.execchain.RetryExec execute
INFO: I/O exception (java.net.SocketException) caught when processing request to {}->http://127.0.0.1:7055: Permission denied: connect
Jan 12, 2015 10:39:40 PM org.apache.http.impl.execchain.RetryExec execute
INFO: Retrying request to {}->http://127.0.0.1:7055
Jan 12, 2015 10:39:40 PM org.apache.http.impl.execchain.RetryExec execute
INFO: I/O exception (java.net.SocketException) caught when processing request to {}->http://127.0.0.1:7055: Permission denied: connect
Jan 12, 2015 10:39:40 PM org.apache.http.impl.execchain.RetryExec execute
INFO: Retrying request to {}->http://127.0.0.1:7055
Jan 12, 2015 10:39:40 PM org.apache.http.impl.execchain.RetryExec execute
INFO: I/O exception (java.net.SocketException) caught when processing request to {}->http://127.0.0.1:7055: Permission denied: connect
Jan 12, 2015 10:39:40 PM org.apache.http.impl.execchain.RetryExec execute
INFO: Retrying request to {}->http://127.0.0.1:7055
Exception in thread "main" org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died.
Build info: version: '2.44.0', revision: '76d78cf', time: '2014-10-23 20:03:00'
System info: host: '****', ip: '**.*.*.*', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_60'
Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:593)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:614)
    at org.openqa.selenium.remote.RemoteWebDriver.quit(RemoteWebDriver.java:468)
    at scrape.Scraper.killInstance(Scraper.java:162)
    at scrape.Updater.main(Updater.java:93)
Caused by: java.net.SocketException: Permission denied: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:72)
    at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:123)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:318)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
    at org.openqa.selenium.remote.HttpCommandExecutor.fallBackExecute(HttpCommandExecutor.java:215)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:184)
    at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.execute(NewProfileExtensionConnection.java:165)
    at org.openqa.selenium.firefox.FirefoxDriver$LazyCommandExecutor.execute(FirefoxDriver.java:362)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:572)
    ... 4 more

如何防止此错误或至少捕获错误并有效处理它?<​​/p>

最佳答案

发生 UnreachableBrowserExceptions 的原因有多种 - 最明显的是浏览器被关闭(无论是在代码中还是在 GUI 中物理关闭),然后代码尝试访问它。通常,就像您的情况一样,它们是由套接字错误引起的。这可能意味着很多事情 - 您的程序尝试打开太多套接字,它无法连接到远程网站,等等。

在这种情况下,我建议做的是等待一小段时间,然后重试以查看是否仍然引发异常。有时这些情况会自行解决并且您的程序可以恢复。

这里有一些代码可以做到这一点。只要抛出 UnreachableBrowserException 并且重试次数低于您设置的某个限制,它就会不断重试。如果达到重试限制并且仍然抛出异常,它将关闭浏览器并重新启动,将重试计数重置为 0。还有一个重新启动计数器,以确保如果由于某种原因重新启动浏览器不会帮助,您不会无休止地循环运行代码 ->异常 ->等待 ->重试 ->达到重试限制,重新启动浏览器 ->运行代码 ->异常。这里,超过重启限制(或者成功访问浏览器)就会跳出循环。

如果您需要更多帮助,请告诉我。希望这对您有帮助!

WebDriver driver = new FirefoxDriver(); //or whatever you're using
boolean worked = false;
int numredos = 0;
final int REDO_LIMIT = 3; //or however many times you want to retry before giving up
final int RESTART_LIMIT = 3; //or however many times you want to restart the browser b/f terminating    
int numrestarts = 0;
boolean restart = false;
do
{
  try{

       if(restart)
       {
         driver = new FirefoxDriver();
         numrestarts++;
       }

    //RUN YOUR BROWSER CODE HERE
      worked = true;                                                        
     }

  //if the browser becomes unreachable (probably b/c of a socket issue), 
// write the error to the log and then sleep for 10 seconds
//if we've already retried the set limit number of times, restart the browser and try again
   catch (UnreachableBrowserException ube)
   {
     worked = false;
     if(numredos >= REDO_LIMIT)
     {
       //if you've already restarted the browser too many times, it will set it to null
       //and return an error code. If not, it will set the restart flag so it will be restarted on the next iteration.

         //try quitting. If it can't do it, it's already dead; just set it to null 
         //(set it to null either way, just in case)
          try
           {
            driver.quit();
           }
          catch(Exception j)
           {
            errorwriter.println(j);
           }

          driver = null;

          if(numrestarts < RESTART_LIMIT)
           {
             //log that you're restarting the driver (not coded here), then set the restart flag to true. This will cause the browser to be restarted after falling out of the catch block
             numredos = 0;
             restart = true;
           }

       }

      else
      {
        //print details of the exception to the error file
        errorfile.println("\n\n\n");
        //timestamp, and some exception details - you can decide which you want
        errorfile.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()));
        errorfile.println(s.getClass());
        errorfile.println(s.getMessage());
        errorfile.println("Cause: " + s.getCause());
        errorfile.flush();

        //now sleep for some number of seconds - here 10
        try
        {
         TimeUnit.SECONDS.sleep(10);
        }

        catch(InterruptedException e)
        {
          System.out.println("waiting after socket crash interrupted");
        }

       numredos++;
      }

    }

}while(!worked && numredos <= REDO_LIMIT && numrestarts <= RESTART_LIMIT);

关于java - Selenium 偶尔出现 UnreachableBrowserException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27926058/

相关文章:

java - 编写快速 CRUD 应用程序的最快方法。当我有一些可重用的 Java 代码时?

java - java打印数组越界

xml - 如何配置 testng.xml 配置文件以使用注释前后运行多个测试

c# - 我自己的异常处理程序仍然抛出异常并使应用程序崩溃

Java 枚举返回 Null

javascript - 你如何在 Javascript 中抛出自定义错误类?

java - 在自己的线程中执行类

java - 如何用java制作字典?

java - 如何使用java在selenium web驱动程序中一一检查所有复选框?

php - php中的behat、mink和selenium有什么区别