Python selenium webdriver-驱动程序突然”;“死亡”;可以';t退出,获取当前url,打开页面 有时候,在我的脚本的中间,我的WebDebug实例会死掉!
从那时起,我不能调用它的任何方法 一些例子:Python selenium webdriver-驱动程序突然”;“死亡”;可以';t退出,获取当前url,打开页面 有时候,在我的脚本的中间,我的WebDebug实例会死掉!,python,selenium,webdriver,urllib2,selenium-webdriver,Python,Selenium,Webdriver,Urllib2,Selenium 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 cur
>>> 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>
>>spsel.driver.current\u url
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
当前url中的文件“/usr/local/lib/python2.6/dist packages/selenium/webdriver/remote/webdriver.py”,第414行
返回self.execute(Command.GET_CURRENT_URL)['value']
文件“/usr/local/lib/python2.6/dist packages/selenium/webdriver/remote/webdriver.py”,第151行,在execute中
响应=self.command\u executor.execute(driver\u command,params)
文件“/usr/local/lib/python2.6/dist packages/selenium/webdriver/remote/remote_connection.py”,第280行,在execute中
返回self.\u请求(url,方法=命令\u信息[0],数据=数据)
文件“/usr/local/lib/python2.6/dist packages/selenium/webdriver/remote/remote\u connection.py”,第321行,在请求中
响应=opener.open(请求)
文件“/usr/lib/python2.6/urllib2.py”,第391行,打开
响应=自身打开(请求,数据)
文件“/usr/lib/python2.6/urllib2.py”,第409行,打开
"开放",
文件“/usr/lib/python2.6/urllib2.py”,第369行,在调用链中
结果=func(*args)
文件“/usr/lib/python2.6/urllib2.py”,第1161行,在http\u open中
返回self.do_open(httplib.HTTPConnection,req)
文件“/usr/lib/python2.6/urllib2.py”,第1136行,打开
引发URL错误(err)
urllib2.URLError:
>>>spsel.driver.quit()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/usr/local/lib/python2.6/dist packages/selenium/webdriver/firefox/webdriver.py”,第55行,退出
RemoteWebDriver.quit(自我)
文件“/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/webdriver.py”,第443行,在quit中
self.execute(Command.QUIT)
文件“/usr/local/lib/python2.6/dist packages/selenium/webdriver/remote/webdriver.py”,第151行,在execute中
响应=self.command\u executor.execute(driver\u command,params)
文件“/usr/local/lib/python2.6/dist packages/selenium/webdriver/remote/remote_connection.py”,第280行,在execute中
返回self.\u请求(url,方法=命令\u信息[0],数据=数据)
文件“/usr/local/lib/python2.6/dist packages/selenium/webdriver/remote/remote\u connection.py”,第321行,在请求中
响应=opener.open(请求)
文件“/usr/lib/python2.6/urllib2.py”,第391行,打开
响应=自身打开(请求,数据)
文件“/usr/lib/python2.6/urllib2.py”,第409行,打开
"开放",
文件“/usr/lib/python2.6/urllib2.py”,第369行,在调用链中
结果=func(*args)
文件“/usr/lib/python2.6/urllib2.py”,第1161行,在http\u open中
返回self.do_open(httplib.HTTPConnection,req)
文件“/usr/lib/python2.6/urllib2.py”,第1136行,打开
引发URL错误(err)
urllib2.URLError:
你知道为什么会这样吗?有没有克服这些问题的最佳实践解决方案
我正在考虑偶尔通过try块中的
驱动程序测试活动性。当前url
,如果它抛出异常,则将驱动程序设置为无
,然后重新实例化它。。。但这是一个丑陋的黑客,我不明白为什么需要它。您是否先运行selenium Web服务器
此问题表明Web服务器未运行是问题所在:
然后,解决方案很可能需要首先运行selenium服务器。
在SeleniumRC的下载中,您会发现一个名为selenium-server.jar的文件(从
几个月前,该文件位于SeleniumRC/selenium-server-1.0.3/selenium-
jar)。
在Linux上,您可以使用以下命令在后台运行selenium服务器
java-jar/path/to/selenium-server.jar 2>/dev/null 1>&2&
您将在此处找到有关如何设置服务器的更完整说明
此页面存在类似的问题:
另一个类似的问题:所以这是Firefox中selenium web驱动程序的“连接被拒绝”错误,它是作为Firefox扩展实现的。这里的猜测是扩展中出现了一些问题,或者Firefox停止了扩展中的httpd代码的工作
您可以尝试检查是否有最新且兼容的Firefox版本
此外,在与selenium驱动程序和FF扩展不断斗争之后,您可能还想尝试一些替代浏览器,例如Chrome Web驱动程序(它需要在python代码中进行几行更改)。我已经把它完全去掉了 我使用的是无头JS库。工作起来很有魅力。 (如果你想看到页面,你可以随时进行屏幕拍摄) 我主要在ruby中工作,所以:用poltergeist(这只是capybara的js_驱动程序)替换了capybara webkit 我很肯定会有类似的解决办法。
也许这并不能回答您的问题,但它将提供一个关于js测试的不同视角。我也遇到过同样的问题,或者至少我认为是这样。在我这边,浏览器(Chrome)会被阻塞,当在终端中手动停止进程时,我会得到相同的错误 当时我正在使用Django的LiveServerTestCase和Splinter并实现以下方法:
@classmethod
def setUpClass(cls):
cls.browser = Browser()
super(MyClass, cls).setUpClass()
@classmethod
def tearDownClass(cls):
cls.browser.quit()
super(MyClass, cls).tearDownClass()
这将为类运行的所有测试只创建一个驱动程序。我也把所有的测试放在一个方法中
无论如何,这将导致司机在某一点阻塞
然后我转而实现Setup和TearDown方法,实例化/退出其中的驱动程序。然后为类中的每个测试(metho)创建并退出驱动程序。我还将我的测试分为几种方法
在那之后,一切都会顺利进行,而我也在做同样的事情。因此,问题似乎主要是用一个司机做所有事情
作为提示,您还可以在安装方法中实现登录功能,这样您的驱动程序就可以在每次测试中登录
@classmethod
def setUpClass(cls):
cls.browser = Browser()
super(MyClass, cls).setUpClass()
@classmethod
def tearDownClass(cls):
cls.browser.quit()
super(MyClass, cls).tearDownClass()
from splinter import Browser
from django.test import LiveServerTestCase
from django.core.urlresolvers import reverse
class MySeleniumTests(LiveServerTestCase):
fixtures = ['initial_data.json']
def setUp(self):
#fire up your driver
self.browser = Browser('chrome')
#login
self.browser.visit('%s%s' % (self.live_server_url, reverse('home')))
self.assertEquals(self.browser.url, '%s%s' % (self.live_server_url,'/accounts/login/?next=%s' % reverse('home')))
self.browser.fill_form({'username': 'test', 'password': 'test'})
self.browser.find_by_tag('button').first.click()
self.assertEquals(self.browser.url, '%s%s' % reverse('home')))
def tearDown(self):
#quit your driver
self.browser.quit()