Python MaxRetryError:HTTPConnectionPool:超过最大重试次数(由协议错误(';连接中止。';,错误(111,';连接被拒绝';)引起)
我有一个问题:我想测试“选择”和“输入”。我可以像下面的代码那样编写它吗 原始代码:Python MaxRetryError:HTTPConnectionPool:超过最大重试次数(由协议错误(';连接中止。';,错误(111,';连接被拒绝';)引起),python,selenium,selenium-webdriver,python-requests,urllib3,Python,Selenium,Selenium Webdriver,Python Requests,Urllib3,我有一个问题:我想测试“选择”和“输入”。我可以像下面的代码那样编写它吗 原始代码: 12 class Sinaselecttest(unittest.TestCase): 13 14 def setUp(self): 15 binary = FirefoxBinary('/usr/local/firefox/firefox') 16 self.driver = webdriver.Firefox(firefox_binary=binary)
12 class Sinaselecttest(unittest.TestCase):
13
14 def setUp(self):
15 binary = FirefoxBinary('/usr/local/firefox/firefox')
16 self.driver = webdriver.Firefox(firefox_binary=binary)
17
18 def test_select_in_sina(self):
19 driver = self.driver
20 driver.get("https://www.sina.com.cn/")
21 try:
22 WebDriverWait(driver,30).until(
23 ec.visibility_of_element_located((By.XPATH,"/html/body/div[9]/div/div[1]/form/div[3]/input"))
24 )
25 finally:
26 driver.quit()
# #测试select功能
27 select=Select(driver.find_element_by_xpath("//*[@id='slt_01']")).select_by_value("微博")
28 element=driver.find_element_by_xpath("/html/body/div[9]/div/div[1]/form/div[3]/input")
29 element.send_keys("杨幂")
30 driver.find_element_by_xpath("/html/body/div[9]/div/div[1]/form/input").click()
31 driver.implicitly_wait(5)
32 def tearDown(self):
33 self.driver.close()
我想测试Selenium的“选择”功能。所以我选择新浪网站选择一个选项,并在textarea中输入文本。然后搜索它。但当我运行此测试时,它出现错误:
Traceback (most recent call last):
File "test_sina_select.py", line 32, in tearDown
self.driver.close()
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 688, in close
self.execute(Command.CLOSE)
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 319, in execute
response = self.command_executor.execute(driver_command, params)
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 376, in execute
return self._request(command_info[0], url, body=data)
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 399, in _request
resp = self._conn.request(method, url, body=body, headers=headers)
File "/usr/lib/python2.7/site-packages/urllib3/request.py", line 68, in request
**urlopen_kw)
File "/usr/lib/python2.7/site-packages/urllib3/request.py", line 81, in request_encode_url
return self.urlopen(method, url, **urlopen_kw)
File "/usr/lib/python2.7/site-packages/urllib3/poolmanager.py", line 247, in urlopen
response = conn.urlopen(method, u.request_uri, **kw)
File "/usr/lib/python2.7/site-packages/urllib3/connectionpool.py", line 617, in urlopen
release_conn=release_conn, **response_kw)
File "/usr/lib/python2.7/site-packages/urllib3/connectionpool.py", line 617, in urlopen
release_conn=release_conn, **response_kw)
File "/usr/lib/python2.7/site-packages/urllib3/connectionpool.py", line 617, in urlopen
release_conn=release_conn, **response_kw)
File "/usr/lib/python2.7/site-packages/urllib3/connectionpool.py", line 597, in urlopen
_stacktrace=sys.exc_info()[2])
File "/usr/lib/python2.7/site-packages/urllib3/util/retry.py", line 271, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=51379): Max retries exceeded with url: /session/2e64d2a1-3c7f-4221-96fe-9d0b1c102195/window (Caused by ProtocolError('Connection aborted.', error(111, 'Connection refused')))
----------------------------------------------------------------------
Ran 1 test in 72.106s
谁能告诉我为什么?谢谢这个错误消息
MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=51379): Max retries exceeded with url: /session/2e64d2a1-3c7f-4221-96fe-9d0b1c102195/window (Caused by ProtocolError('Connection aborted.', error(111, 'Connection refused')))
…表示调用self.driver.close()
方法失败,无法引发MaxRetryError
有几件事:
- 首先也是最重要的是,根据讨论,追溯有些误导。请求包装异常以方便用户。原始异常是显示的消息的一部分
- 请求从不重试(它为urllib3的
设置HTTPConnectionPool
),因此如果没有MaxRetryError和HTTPConnectionPool关键字,错误会更加规范。因此,理想的回溯应该是:retries=0
合并是:ConnectionError(<class 'socket.error'>: [Errno 1111] Connection refused)
结论 升级到Selenium 3.14.1后,您将能够设置超时并查看规范回溯,并能够采取所需的操作
工具书类 一些相关参考资料:- 也有同样的问题。解决方案是使用脚本递归地更改文件夹的所有者。在我的例子中,文件夹有
owner:group,我需要将其更改为root:root
解决方案:ubuntu:ubuntu
sudo chown-R ubuntu:ubuntu/path to your folder
可能重复的@郑佳妮 使用完整的错误堆栈更新问题trace@DebanjanB问题已更新。@Infern0,好的,我会查看它。谢谢。您隐式设置了什么?我认为“选择”函数错误,结果是超时原因。对于您,我将尝试升级我的Selenium并修改我的“选择”。使用Try-and-catch块查找异常 尝试: r=请求。获取(url) 除requests.exceptions.Timeout外: #信息 除requests.exceptions.TooManyRedirects外: #信息 除了requests.exceptions.RequestException作为e: #信息
升起系统出口(e)* Fix ability to set timeout for urllib3 (#6286)