代码:
结果:
PS E:\30.Study\30.自动化测试\99.零基础入门 Python Web 自动化测试\10.seleniumCodePractice> & "C:/Program Files/Python38/python.exe" "e:/30.Study/30.自动化测试/99.零基础入门 Python Web 自动化测试/10.seleniumCodePractice/202006/requests_open_browser.py"
Traceback (most recent call last):
File "C:\Program Files\Python38\lib\site-packages\urllib3\connection.py", line 156, in _new_conn
conn = connection.create_connection(
File "C:\Program Files\Python38\lib\site-packages\urllib3\util\connection.py", line 84, in create_connection
raise err
File "C:\Program Files\Python38\lib\site-packages\urllib3\util\connection.py", line 74, in create_connection
sock.connect(sa)
ConnectionRefusedError: [WinError 10061] 由于目标计算机积极拒绝,无法连接。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files\Python38\lib\site-packages\urllib3\connectionpool.py", line 665, in urlopen
httplib_response = self._make_request(
File "C:\Program Files\Python38\lib\site-packages\urllib3\connectionpool.py", line 387, in _make_request
conn.request(method, url, **httplib_request_kw)
File "C:\Program Files\Python38\lib\http\client.py", line 1230, in request
self._send_request(method, url, body, headers, encode_chunked)
File "C:\Program Files\Python38\lib\http\client.py", line 1276, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "C:\Program Files\Python38\lib\http\client.py", line 1225, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "C:\Program Files\Python38\lib\http\client.py", line 1004, in _send_output
self.send(msg)
File "C:\Program Files\Python38\lib\http\client.py", line 944, in send
self.connect()
File "C:\Program Files\Python38\lib\site-packages\urllib3\connection.py", line 184, in connect
conn = self._new_conn()
File "C:\Program Files\Python38\lib\site-packages\urllib3\connection.py", line 168, in _new_conn
raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x000001D22D03D490>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积
极拒绝,无法连接。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files\Python38\lib\site-packages\requests\adapters.py", line 439, in send
resp = conn.urlopen(
File "C:\Program Files\Python38\lib\site-packages\urllib3\connectionpool.py", line 719, in urlopen
retries = retries.increment(
File "C:\Program Files\Python38\lib\site-packages\urllib3\util\retry.py", line 436, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host=‘127.0.0.1‘, port=4444): Max retries exceeded with url: /wd/hub/session (Caused by NewConnectionError(‘<urllib3.connection.HTTPConnection object at 0x000001D22D03D490>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。‘))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "e:/30.Study/30.自动化测试/99.零基础入门 Python Web 自动化测试/10.seleniumCodePractice/202006/requests_open_browser.py", line 10, in <module>
print(requests.post(url,data).json())
File "C:\Program Files\Python38\lib\site-packages\requests\api.py", line 119, in post
return request(‘post‘, url, data=data, json=json, **kwargs)
File "C:\Program Files\Python38\lib\site-packages\requests\api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Program Files\Python38\lib\site-packages\requests\sessions.py", line 530, in request
resp = self.send(prep, **send_kwargs)
File "C:\Program Files\Python38\lib\site-packages\requests\sessions.py", line 643, in send
r = adapter.send(request, **kwargs)
File "C:\Program Files\Python38\lib\site-packages\requests\adapters.py", line 516, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host=‘127.0.0.1‘, port=4444): Max retries exceeded with url: /wd/hub/session (Caused by NewConnectionError(‘<urllib3.connection.HTTPConnection object at 0x000001D22D03D490>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。‘))
PS E:\30.Study\30.自动化测试\99.零基础入门 Python Web 自动化测试\10.seleniumCodePractice>
原因分析:
【requests.exceptions.ConnectionError: HTTPConnectionPool(host=‘127.0.0.1‘, port=4444)】可知,是连接http失败。即下图中selenium服务没有启动,导致无法连接。
对应方法:
找到"selenium-server-standalone-3.141.59.jar"所在路径,通过"java -jar selenium-server-standalone-3.141.59.jar"启动selenium即可。
验证是否启动成功,通过在浏览器地址栏里输入“http://127.0.0.1:4444/”来验证,成功的话,下图会表示出来:
selenium打开浏览器底层实现原理中的异常4(Failed to establish a new connection: [WinError 10061] 由于目标计算机积 极拒绝,无法连接)