Selenium Firefox webdriver抛出URLError

时间:2012-07-23 19:21:22

标签: python django firefox selenium webdriver

我正在使用python版本的selenium进行django的一些测试,但firefox webdriver在尝试启动新会话时抛出URLError异常。我所做的是:

browser = webdriver.Firefox()

更新:然后我做了一些测试,然后我需要关闭它,因为当前测试完成,但是对于下一个我需要一个新的会话,所以我尝试打开一个新的

browser.close()
capabilities = webdriver.DesiredCapabilities()
browser.start_session(capabilities.FIREFOX)

然后它抛出了这个:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/user/projects/myproject/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 104, in start_session
    'desiredCapabilities': desired_capabilities,
  File "/home/user/projects/myproject/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 153, in execute
    response = self.command_executor.execute(driver_command, params)
  File "/home/user/projects/myproject/local/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 285, in execute
    return self._request(url, method=command_info[0], data=data)
  File "/home/user/projects/myproject/local/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 326, in _request
    response = opener.open(request)
  File "/usr/lib/python2.7/urllib2.py", line 400, in open
    response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 418, in _open
    '_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 1207, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/usr/lib/python2.7/urllib2.py", line 1177, in do_open
    raise URLError(err)
URLError: <urlopen error [Errno 111] Connection refused>

奇怪的是,同样的步骤与Chrome网络驱动程序完美配合。

有谁知道为什么会造成这种情况? 在此先感谢您的任何帮助

1 个答案:

答案 0 :(得分:2)

你不应该使用start_session。 在第一行,您将创建本地firefox浏览器的新实例。 start_session是远程webdriver的一种方法。 您所需要的只是:

driver = webdriver.Firefox() #opens new Firefox browser locally
driver.quit() #kills the session
driver = webdriver.Firefox() #opens new Firefox browser locally

由于你在这里重复代码,你应该在测试类中有这样的东西

class Test(unittest.Testcase):
    def setUp(self):
        self.driver = webdriver.Firefox()
    def tearDown(self):
        self.driver.quit()
    def test_something(self):
        #do some magic test here
    def test_something_else(self):
        #do some other stuff

这样浏览器将在每次测试开始时启动,并在每次测试结束时关闭。