使用selenuim报错 raise MaxRetryError( Max retries exceeded with url:

本文讲述了在使用Selenium操作Chrome时遇到的MaxRetryError,重点在于元素获取需在关闭浏览器前后处理,避免连接失效。解决方案是先获取元素文本再退出浏览器。

使用selenuim报错 raise MaxRetryError(_pool, url, error or ResponseError(cause))

urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host=‘127.0.0.1’, port=53643): Max retries exceeded with url: /session/3ce406f838f141e6d386e6532f7bfa7f/element/fb4d42b8-ce35-46d0-8c30-1eaa6384f682/text (Caused by NewConnectionError(’<urllib3.connection.HTTPConnection object at 0x00000186B6A67460>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。’))

这里主要错误是 Max retries exceeded with url:

记一个bug,使用selenium.webdriver.Chrome().find_XX_XXX()获得的元素,必须在brower.close()或者brower.quit()使用,因为此时的元素是要依赖brower连接的

所以解决办法是把获取的元素的text获取出来,然后再关闭brower
C:\own\app\python_code\.venv\Scripts\python.exe C:\own\app\python_code\work\IBL_click_all\click_all.py Traceback (most recent call last): File "C:\own\app\python_code\.venv\Lib\site-packages\urllib3\connection.py", line 198, in _new_conn sock = connection.create_connection( (self._dns_host, self.port), ...<2 lines>... socket_options=self.socket_options, ) File "C:\own\app\python_code\.venv\Lib\site-packages\urllib3\util\connection.py", line 85, in create_connection raise err File "C:\own\app\python_code\.venv\Lib\site-packages\urllib3\util\connection.py", line 73, in create_connection sock.connect(sa) ~~~~~~~~~~~~^^^^ ConnectionRefusedError: [WinError 10061] 対象のコンピューターによって拒否されたため、接続できませんでした。 The above exception was the direct cause of the following exception: Traceback (most recent call last): File "C:\own\app\python_code\.venv\Lib\site-packages\urllib3\connectionpool.py", line 787, in urlopen response = self._make_request( conn, ...<10 lines>... **response_kw, ) File "C:\own\app\python_code\.venv\Lib\site-packages\urllib3\connectionpool.py", line 493, in _make_request conn.request( ~~~~~~~~~~~~^ method, ^^^^^^^ ...<6 lines>... enforce_content_length=enforce_content_length, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ) ^ File "C:\own\app\python_code\.venv\Lib\site-packages\urllib3\connection.py", line 494, in request self.endheaders() ~~~~~~~~~~~~~~~^^ File "C:\own\app\python\Lib\http\client.py", line 1333, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\own\app\python\Lib\http\client.py", line 1093, in _send_output self.send(msg) ~~~~~~~~~^^^^^ File "C:\own\app\python\Lib\http\client.py", line 1037, in send self.connect() ~~~~~~~~~~~~^^ File "C:\own\app\python_code\.venv\Lib\site-packages\urllib3\connection.py", line 325, in connect self.sock = self._new_conn() ~~~~~~~~~~~~~~^^ File "C:\own\app\python_code\.venv\Lib\site-packages\urllib3\connection.py", line 213, in _new_conn raise NewConnectionError( self, f"Failed to establish a new connection: {e}" ) from e urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x00000219527A0E90>: Failed to establish a new connection: [WinError 10061] 対象のコンピューターによって拒否されたため、接続できませんでした。 The above exception was the direct cause of the following exception: Traceback (most recent call last): File "C:\own\app\python_code\work\IBL_click_all\click_all.py", line 25, in <module> driver = webdriver.Remote( command_executor='http://localhost:4723', # Appium Server 地址 options=options # 使用 options 替代 desired_caps ) File "C:\own\app\python_code\.venv\Lib\site-packages\appium\webdriver\webdriver.py", line 249, in __init__ super().__init__( ~~~~~~~~~~~~~~~~^ command_executor=command_executor, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...<3 lines>... client_config=client_config, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ) ^ File "C:\own\app\python_code\.venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 261, in __init__ self.start_session(capabilities) ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^ File "C:\own\app\python_code\.venv\Lib\site-packages\appium\webdriver\webdriver.py", line 342, in start_session response = self.execute(RemoteCommand.NEW_SESSION, w3c_caps) File "C:\own\app\python_code\.venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 451, in execute response = cast(RemoteConnection, self.command_executor).execute(driver_command, params) File "C:\own\app\python_code\.venv\Lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 404, in execute return self._request(command_info[0], url, body=data) ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\own\app\python_code\.venv\Lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 428, in _request response = self._conn.request(method, url, body=body, headers=headers, timeout=self._client_config.timeout) File "C:\own\app\python_code\.venv\Lib\site-packages\urllib3\_request_methods.py", line 143, in request return self.request_encode_body( ~~~~~~~~~~~~~~~~~~~~~~~~^ method, url, fields=fields, headers=headers, **urlopen_kw ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ) ^ File "C:\own\app\python_code\.venv\Lib\site-packages\urllib3\_request_methods.py", line 278, in request_encode_body return self.urlopen(method, url, **extra_kw) ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\own\app\python_code\.venv\Lib\site-packages\urllib3\poolmanager.py", line 459, in urlopen response = conn.urlopen(method, u.request_uri, **kw) File "C:\own\app\python_code\.venv\Lib\site-packages\urllib3\connectionpool.py", line 871, in urlopen return self.urlopen( ~~~~~~~~~~~~^ method, ^^^^^^^ ...<13 lines>... **response_kw, ^^^^^^^^^^^^^^ ) ^ File "C:\own\app\python_code\.venv\Lib\site-packages\urllib3\connectionpool.py", line 871, in urlopen return self.urlopen( ~~~~~~~~~~~~^ method, ^^^^^^^ ...<13 lines>... **response_kw, ^^^^^^^^^^^^^^ ) ^ File "C:\own\app\python_code\.venv\Lib\site-packages\urllib3\connectionpool.py", line 871, in urlopen return self.urlopen( ~~~~~~~~~~~~^ method, ^^^^^^^ ...<13 lines>... **response_kw, ^^^^^^^^^^^^^^ ) ^ File "C:\own\app\python_code\.venv\Lib\site-packages\urllib3\connectionpool.py", line 841, in urlopen retries = retries.increment( method, url, error=new_e, _pool=self, _stacktrace=sys.exc_info()[2] ) File "C:\own\app\python_code\.venv\Lib\site-packages\urllib3\util\retry.py", line 519, in increment raise MaxRetryError(_pool, url, reason) from reason # type: ignore[arg-type] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=4723): Max retries exceeded with url: /session (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x00000219527A0E90>: Failed to establish a new connection: [WinError 10061] 対象のコンピューターによって拒否されたため、接続できませんでした。')) Process finished with exit code 1
10-16
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值