有时,在我的脚本中间,我的 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>
知道为什么会这样吗?有什么克服困难的最佳实践解决方案吗?
我正在考虑偶尔在 try block 中通过 driver.current_url
测试 active ,如果它抛出异常,则将驱动程序设置为 None
,并且然后重新实例化它...但这是一个丑陋的 hack,我不明白为什么需要它。
最佳答案
在与 selenium 驱动程序和 FF 扩展分手的无休止斗争之后。我已将其完全删除。
我使用 http://www.phantomjs.org/这是 headless 的 JS 库。像魅力一样工作。 (如果你想看页面,你可以随时进行屏幕截图)
我主要在 ruby 工作,所以:用 poltergeist 替换了 capybara-webkit(这只是 capybara 的 js_driver)
我很确定会有类似的解决方案。 也许这不能回答您的问题,但它会提供有关 js 测试的不同外观。
关于Python selenium webdriver - 驱动程序突然出现 "dies"并且无法退出,获取 current_url,打开页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9593775/