gpt4 book ai didi

python - Selenium Firefox Webdriver 抛出 URLError

转载 作者:行者123 更新时间:2023-12-01 05:57:59 31 4
gpt4 key购买 nike

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

browser = webdriver.Firefox()

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

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 Webdriver 中运行得非常好。

有谁知道为什么会出现这种情况吗?预先感谢您的帮助

最佳答案

您不必为此使用 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

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

关于python - Selenium Firefox Webdriver 抛出 URLError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11619063/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com