selenium打开浏览器底层实现原理中的异常4(Failed to establish a new connection: [WinError 10061] 由于目标计算机积 极拒绝,无法连接)

Posted hadas

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了selenium打开浏览器底层实现原理中的异常4(Failed to establish a new connection: [WinError 10061] 由于目标计算机积 极拒绝,无法连接)相关的知识,希望对你有一定的参考价值。

代码:

#coding=utf-8
import requests
import json
url=‘http://127.0.0.1:4444/wd/hub/session‘
data = json.dumps({
    ‘desiredCapabilities‘:{
        ‘browserName‘:‘MicrosoftEdge‘
    }
})
print(requests.post(url,data).json())

结果:

PS E:30.Study30.自动化测试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 FilesPython38libsite-packagesurllib3connection.py", line 156, in _new_conn
    conn = connection.create_connection(
  File "C:Program FilesPython38libsite-packagesurllib3utilconnection.py", line 84, in create_connection
    raise err
  File "C:Program FilesPython38libsite-packagesurllib3utilconnection.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 FilesPython38libsite-packagesurllib3connectionpool.py", line 665, in urlopen
    httplib_response = self._make_request(
  File "C:Program FilesPython38libsite-packagesurllib3connectionpool.py", line 387, in _make_request    
    conn.request(method, url, **httplib_request_kw)
  File "C:Program FilesPython38libhttpclient.py", line 1230, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:Program FilesPython38libhttpclient.py", line 1276, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:Program FilesPython38libhttpclient.py", line 1225, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:Program FilesPython38libhttpclient.py", line 1004, in _send_output
    self.send(msg)
  File "C:Program FilesPython38libhttpclient.py", line 944, in send
    self.connect()
  File "C:Program FilesPython38libsite-packagesurllib3connection.py", line 184, in connect
    conn = self._new_conn()
  File "C:Program FilesPython38libsite-packagesurllib3connection.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 FilesPython38libsite-packages equestsadapters.py", line 439, in send
    resp = conn.urlopen(
  File "C:Program FilesPython38libsite-packagesurllib3connectionpool.py", line 719, in urlopen
    retries = retries.increment(
  File "C:Program FilesPython38libsite-packagesurllib3util etry.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 FilesPython38libsite-packages equestsapi.py", line 119, in post
    return request(‘post‘, url, data=data, json=json, **kwargs)
  File "C:Program FilesPython38libsite-packages equestsapi.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:Program FilesPython38libsite-packages equestssessions.py", line 530, in request
    resp = self.send(prep, **send_kwargs)
  File "C:Program FilesPython38libsite-packages equestssessions.py", line 643, in send
    r = adapter.send(request, **kwargs)
  File "C:Program FilesPython38libsite-packages equestsadapters.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.Study30.自动化测试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] 由于目标计算机积 极拒绝,无法连接)的主要内容,如果未能解决你的问题,请参考以下文章

selenium打开浏览器底层实现原理中的异常3.1(org.openqa.selenium.SessionNotCreatedException: Unable to create session)

selenium打开浏览器底层实现原理中的异常1(TypeError: dump() missing 1 required positional argument: 'fp')

搞懂webdriver的底层原理,才敢说自己懂自动化!

浅谈python中selenium库调动webdriver驱动浏览器的实现原理

二 selenium原理以及如何操作浏览器

线程“主”org.openqa.selenium.NoAlertPresentException 中的异常:没有打开警报