硒偶尔UnreachableBrowserException [英] Selenium occasional UnreachableBrowserException

查看:166
本文介绍了硒偶尔UnreachableBrowserException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to access several websites by using Selenium in Java. Occasionally, I get an UnreachableBrowserException. I have read many threads about this error but it seems like there are many different causes of the error. I get the error about 1% of the time when I attempt to access a new page and I cannot find any similarities between occurrences. I currently am using Firefox, however I have also tried Internet Explorer and experienced similar errors. I am only opening one page at a time and have tried using the same window and completely quitting the driver before trying to access another page and either way the error still occurs. It is important to note I do not always get this error sometimes my code can run without this occurring. Here is the error message:

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

如何防止此错误或至少catc h的错误和处理它有效?

How can I prevent this error or at least catch the error and deal with it effectively?

推荐答案

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

UnreachableBrowserExceptions can happen for multiple reasons - the most obvious is that the browser was closed, either in code or physically in the GUI, and then the code attempted to access it. Often, like in your case, they are caused by socket errors. This can mean, again, multiple things - your program tried to open too many sockets, it couldn't connect to a remote website, and others.

我在这样的情况下建议做的是等待很短的时间,然后重试以查看是否仍然抛出异常。有时这些情况会自动解决,您的程序可以恢复。

What I would suggest doing in a situation like this is waiting a short time, then retrying to see if the exception is still thrown. Sometimes these situations resolve themselves and your program can recover.

这是一些代码。只要抛出UnreachableBrowserException并且重试次数低于您设置的限制,它就会重试。如果它遇到重试限制,并且异常仍然被抛出,它将关闭浏览器并重新启动它,将重试计数重置为0.还有一个重新启动计数器,以确保如果由于某些原因重新启动浏览器,则不会帮助,你不要循环运行代码循环 - >异常 - >等待 - >重试 - >命中重试限制,重新启动浏览器 - >运行代码 - >异常。在这里,超过重新启动限制(或成功访问浏览器)将会突破循环。

Here is some code to do that. It keeps retrying as long as the UnreachableBrowserException is thrown and the number of retries is below some limit that you set. If it hits the retry limit and the exception is still being thrown, it closes the browser and restarts it, resetting the retry count to 0. There is also a restart counter, to make sure that if for some reason restarting the browser doesn't help, you don't loop endlessly through running code -> exception -> wait -> retry -> hit retry limit, restart browser -> run code -> exception. Here, exceeding the restart limit (or successfully accessing the browser) will break out of the loop.

如果您需要更多帮助,请告知我。希望这是有帮助的!

If you want more help, let me know.Hope this is helpful!

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);

这篇关于硒偶尔UnreachableBrowserException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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