Python selenium webdriver - 驱动程序突然"死",无法退出,获取current_url,打开页面

Zac*_*urt 7 python selenium webdriver urllib2 selenium-webdriver

有时,在我的脚本中间,我的webdriver实例将会!

从那以后,我无法调用它的任何方法.

一些例子:

>>> spsel.driver.current_url
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 414, in current_url
    return self.execute(Command.GET_CURRENT_URL)['value']
  File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 151, in execute
    response = self.command_executor.execute(driver_command, params)
  File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/remote_connection.py", line 280, in execute
    return self._request(url, method=command_info[0], data=data)
  File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/remote_connection.py", line 321, in _request
    response = opener.open(request)
  File "/usr/lib/python2.6/urllib2.py", line 391, in open
    response = self._open(req, data)
  File "/usr/lib/python2.6/urllib2.py", line 409, in _open
    '_open', req)
  File "/usr/lib/python2.6/urllib2.py", line 369, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.6/urllib2.py", line 1161, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/usr/lib/python2.6/urllib2.py", line 1136, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error [Errno 111] Connection refused>



>>> spsel.driver.quit()

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/firefox/webdriver.py", line 55, in quit
    RemoteWebDriver.quit(self)
  File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 443, in quit
    self.execute(Command.QUIT)
  File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 151, in execute
    response = self.command_executor.execute(driver_command, params)
  File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/remote_connection.py", line 280, in execute
    return self._request(url, method=command_info[0], data=data)
  File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/remote_connection.py", line 321, in _request
    response = opener.open(request)
  File "/usr/lib/python2.6/urllib2.py", line 391, in open
    response = self._open(req, data)
  File "/usr/lib/python2.6/urllib2.py", line 409, in _open
    '_open', req)
  File "/usr/lib/python2.6/urllib2.py", line 369, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.6/urllib2.py", line 1161, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/usr/lib/python2.6/urllib2.py", line 1136, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error [Errno 111] Connection refused>
Run Code Online (Sandbox Code Playgroud)

任何想法为什么会出现这种情况?任何克服的最佳实践解决方案?

我想偶尔driver.current_url在try块中测试liveness ,如果它抛出异常,然后设置驱动程序None,然后重新实例化它......但这是一个丑陋的黑客,我不明白为什么它是需要.

two*_*ace 1

经过与 selenium 驱动程序和 FF 扩展的无休止的斗争之后。我已经把它完全删除了。

我使用http://www.phantomjs.org/这是无头 JS 库。就像魅力一样起作用。(如果您想查看该页面,您可以随时进行屏幕拍摄)

我主要在 ruby​​ 中工作,所以:用poltergeist 替换 capybara-webkit (这只是 capybara 的 js_driver )

我很确定会有类似的解决方案。也许这不能回答您的问题,但它将提供有关 js 测试的不同外观。

  • 哈哈,我们在 PhantomJS 中遇到了这个问题,但我们所做的只是检查 Webdriver 是否没有响应,如果没有响应,我们就杀死它并启动一个新的。到目前为止效果很好! (3认同)