简体   繁体   English

Python3 ConnectionRefusedError selenium.webdriver.Remote 在 Ubuntu 服务器上

[英]Python3 ConnectionRefusedError selenium.webdriver.Remote on Ubuntu Server

from selenium import webdriver
url = "http://localhost:4444/wd/hub"
cap = {'browserName': 'chrome'}
driver = webdriver.Remote(command_executor=url, desired_capabilities=cap)

I try this on Ubuntu Server, but i have error我在 Ubuntu 服务器上尝试这个,但我有错误

Traceback (most recent call last):

File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 156, in _new_conn
conn = connection.create_connection(

File "/usr/lib/python3/dist-packages/urllib3/util/connection.py", line 84, in create_connection
raise err

 File "/usr/lib/python3/dist-packages/urllib3/util/connection.py", line 74, in create_connection

 sock.connect(sa)

ConnectionRefusedError: [Errno 111] Connection refused


During handling of the above exception, another exception occurred:


Traceback (most recent call last):

  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 665, in urlopen
httplib_response = self._make_request(

  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 387, in _make_request
conn.request(method, url, **httplib_request_kw)

  File "/usr/lib/python3.8/http/client.py", line 1240, in request
self._send_request(method, url, body, headers, encode_chunked)

  File "/usr/lib/python3.8/http/client.py", line 1286, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)

  File "/usr/lib/python3.8/http/client.py", line 1235, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)

  File "/usr/lib/python3.8/http/client.py", line 1006, in _send_output
self.send(msg)

  File "/usr/lib/python3.8/http/client.py", line 946, in send
self.connect()

  File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 184, in connect
conn = self._new_conn()

  File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 168, in _new_conn
raise NewConnectionError(

urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7f6377a9f8e0>: Failed to establish a new connection: [Errno 111] Connection refused


During handling of the above exception, another exception occurred:


Traceback (most recent call last):

File "test.py", line 89, in <module>
test_some.testMethod()

File "test.py", line 21, in testMethod
driver = webdriver.Remote(command_executor=selenium_url, desired_capabilities=caps)

File "/home/vl/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)

File "/home/vl/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)

File "/home/vl/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 319, in execute
response = self.command_executor.execute(driver_command, params)

File "/home/vl/.local/lib/python3.8/site-packages/selenium/webdriver/remote/remote_connection.py", line 374, in execute
return self._request(command_info[0], url, body=data)

File "/home/vl/.local/lib/python3.8/site-packages/selenium/webdriver/remote/remote_connection.py", line 402, in _request
resp = http.request(method, url, body=body, headers=headers)

File "/usr/lib/python3/dist-packages/urllib3/request.py", line 79, in request
return self.request_encode_body(

File "/usr/lib/python3/dist-packages/urllib3/request.py", line 171, in request_encode_body
return self.urlopen(method, url, **extra_kw)

 File "/usr/lib/python3/dist-packages/urllib3/poolmanager.py", line 330, in urlopen
response = conn.urlopen(method, u.request_uri, **kw)

File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 747, in urlopen
return self.urlopen(

File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 747, in urlopen
return self.urlopen(

File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 747, in urlopen
return self.urlopen(

File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 719, in urlopen
retries = retries.increment(

File "/usr/lib/python3/dist-packages/urllib3/util/retry.py", line 436, in increment

raise MaxRetryError(_pool, url, error or ResponseError(cause))

urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=4444): Max retries exceeded with url: /wd/hub/session (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f6377a9f8e0>: Failed to establish a new connection: [Errno 111] Connection refused'))

i have Chrome on Ubuntu Server我在 Ubuntu 服务器上有 Chrome

further idea is to open the link, after a while exit the window / browser进一步的想法是打开链接,一段时间后退出 window / 浏览器

is it possible to run it successfully?有可能成功运行吗?

or are there other libraries to solve this problem?还是有其他库可以解决这个问题?

I do not know what else to write so that I can post this question here:/我不知道还要写什么,以便我可以在这里发布这个问题:/

ConnectionRefusedError: [Errno 111] Connection refused is showing that your script is unable to access the remote Selenium server. ConnectionRefusedError: [Errno 111] Connection refused denied 表明您的脚本无法访问远程 Selenium 服务器。

In order to use a remote web driver, selenium server should be running on that remote Ubuntu server, and chrome driver executable file should be available in the $PATH .为了使用远程 web 驱动程序, selenium 服务器应该在远程 Ubuntu 服务器上运行,并且chrome driver可执行文件应该在$PATH中可用。 You can read further about it here您可以在此处进一步了解它

So, make sure:因此,请确保:

  • You have downloaded the chrome driver from the official site and placed it in the $PATH您已经从官方网站下载了chrome driver并将其放在$PATH
  • You have downloaded the Selenium server from the official site and have it running on the remote Ubuntu server.您已经从官方网站下载了Selenium server ,并让它在远程 Ubuntu 服务器上运行。 You can simply start the Selenium server like this:您可以像这样简单地启动 Selenium 服务器:
java -jar selenium-server-standalone-3.XXX.XX.jar

Selenium server by default binds to all available network interfaces with a default base URL wd/hub as you have shown in the code snippet: Selenium 服务器默认绑定到具有默认基础 URL wd/hub的所有可用网络接口,如代码片段所示:

url = "http://[HOST]:4444/wd/hub"

If you are going to use your script on the same server where the Selenium server is running, localhost should be alright.如果您要在运行 Selenium 服务器的同一台服务器上使用您的脚本,则localhost应该没问题。 But if you are going to use it from a remote location you need to provide the correct IP/domain of the Selenium server.但是,如果您要从远程位置使用它,您需要提供 Selenium 服务器的正确 IP/域。

I would suggest If you intend to use your script locally on a Linux server machine (non-GUI environment), you can utilize the headless mode of Chrome/Firefox to achieve the same thing.我建议如果您打算在 Linux 服务器机器(非 GUI 环境)上本地使用您的脚本,您可以利用 Chrome/Firefox 的无头模式来实现相同的目的。 You can use the headless option like this:您可以像这样使用headless选项:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument("--headless")

driver = webdriver.Chrome()
driver.get('https://google.com.pk')
print(driver.page_source.encode("utf-8"))
driver.quit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM