1

我正在使用 selenium 的 python 版本对 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 网络驱动程序上运行得很好。

有谁知道为什么会这样?提前感谢您的帮助

4

1 回答 1

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

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

于 2012-08-16T04:44:01.097 回答