有时,在我的脚本中间,我的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块中测试driver.current_url
的活跃度,如果它抛出异常,然后将驱动程序设置为None
,然后重新实例化它......但这是一个丑陋的黑客,我不明白为什么需要它。
答案 0 :(得分:1)
经过无休止的努力与硒司机和FF扩展分手。我完全删除了它。
我使用无头JS lib的http://www.phantomjs.org/。像魅力一样工作。 (我想看看页面,你可以随时进行屏幕拍摄)
我主要在红宝石中工作:用poltergeist取代capybara-webkit(这只是水豚的js_driver)
我很确定会有类似的解决方案。 也许这不能回答你的问题,但它会提供关于js测试的不同看法。
答案 1 :(得分:0)
您是否先运行selenium webserver?
此问题表明网络服务器未运行是问题所在:Selenium in Python
then the solution is most likely that you need get the selenium server running first. In the download for SeleniumRC you will find a file called selenium-server.jar (as of a few months ago, that file was located at SeleniumRC/selenium-server-1.0.3/selenium- server.jar). On Linux, you could run the selenium server in the background with the command java -jar /path/to/selenium-server.jar 2>/dev/null 1>&2 &
您可以在http://seleniumhq.org/docs/05_selenium_rc.html#installation
找到有关如何设置服务器的更完整说明此页面存在类似问题:http://johnmudd.infogami.com/blog/5be6
另一个类似的问题:How do you connect remotely using Python + Webdriver
答案 2 :(得分:0)
因此,Firefox中的selenium web驱动程序出现了“连接被拒绝”错误,该错误是作为Firefox扩展实现的。这里的猜测是扩展中的一些问题,或者Firefox会阻止扩展中的httpd代码工作。
您可以尝试检查是否有最新的web driver extension和兼容的Firefox版本。
你也可以尝试一些替代浏览器,例如Chrome Web Driver(它需要在您的python代码中进行几行更改)
答案 3 :(得分:0)
我遇到了同样的问题,或者至少我是这么认为的。在我这边浏览器(Chrome)将被阻止,当手动停止终端中的进程时,我会得到相同的URLError。
当时我正在使用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方法实例化/退出其中的驱动程序。然后,这将为类中的每个测试(方法)创建并退出驱动程序。我还将测试分解为几种方法。
之后一切都会运行得很好,而我正在做同样的事情。所以问题似乎主要是用一个驱动程序做所有事情。
作为提示,您可以将登录内容实现到Setup方法中,以便您的驱动程序在每次测试时都会登录,这是必要的,因为退出驱动程序也会刷新会话。
这是最终结果:
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()