pythonpython-3.xgoogle-chromeselenium-webdriverselenium-chromedriver

Seleniun Webdriver Failed to establish a new connection


Been knocking my head around persistent problems with trying to get Selenium Web Driver up and running. The following code is what I have. Also sent it to friends and they've been able to run it after installing the mandatory selenium & webdriver-manager files (Same ones I am using).

Seems like this is some kind of networking issue? I've searched round but stuck with limited to zero understanding of networking to make any progress with this issue. I have included the full error log below. If there is anything else I can add let me know.

from selenium import webdriver
from selenium.webdriver.chrome.service import  Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager

options = Options()
#options.add_experimental_option("detact", True)

#crashes here on next line
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.get("https://www.google.com/")

Error Log:

File
"C:\dev\devtools\apps\python3\python-3.9.0\lib\site-packages\urllib3\connection.py",
line 174, in _new_conn conn = connection.create_connection( File
"C:\dev\devtools\apps\python3\python-3.9.0\lib\site-packages\urllib3\util\connection.py",
line 72, in create_connection for res in socket.getaddrinfo(host,
port, family, socket.SOCK_STREAM): File
"C:\dev\devtools\apps\python3\python-3.9.0\lib\socket.py", line 953,
in getaddrinfo for res in _socket.getaddrinfo(host, port, family,
type, proto, flags): socket.gaierror: [Errno 11001] getaddrinfo failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File
"C:\dev\devtools\apps\python3\python-3.9.0\lib\site-packages\urllib3\connectionpool.py",
line 703, in urlopen httplib_response = self._make_request( File
"C:\dev\devtools\apps\python3\python-3.9.0\lib\site-packages\urllib3\connectionpool.py",
line 386, in _make_request self._validate_conn(conn) File
"C:\dev\devtools\apps\python3\python-3.9.0\lib\site-packages\urllib3\connectionpool.py",
line 1040, in _validate_conn conn.connect() File
"C:\dev\devtools\apps\python3\python-3.9.0\lib\site-packages\urllib3\connection.py",
line 358, in connect self.sock = conn = self._new_conn() File
"C:\dev\devtools\apps\python3\python-3.9.0\lib\site-packages\urllib3\connection.py",
line 186, in _new_conn raise NewConnectionError(
urllib3.exceptions.NewConnectionError:
<urllib3.connection.HTTPSConnection object at 0x0000023F38A7D7C0>:
Failed to establish a new connection: [Errno 11001] getaddrinfo failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File
"C:\dev\devtools\apps\python3\python-3.9.0\lib\site-packages\requests\adapters.py",
line 489, in send resp = conn.urlopen( File
"C:\dev\devtools\apps\python3\python-3.9.0\lib\site-packages\urllib3\connectionpool.py",
line 785, in urlopen retries = retries.increment( File
"C:\dev\devtools\apps\python3\python-3.9.0\lib\site-packages\urllib3\util\retry.py",
line 592, in increment raise MaxRetryError(_pool, url, error or
ResponseError(cause)) urllib3.exceptions.MaxRetryError:
HTTPSConnectionPool(host='chromedriver.storage.googleapis.com',
port=443): Max retries exceeded with url: /LATEST_RELEASE_114.0.5735
(Caused by NewConnectionError('<urllib3.connection.HTTPSConnection
object at 0x0000023F38A7D7C0>: Failed to establish a new connection:
[Errno 11001] getaddrinfo failed'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File
"C:\dev\devtools\apps\python3\python-3.9.0\lib\site-packages\requests\api.py",
line 59, in request return session.request(method=method, url=url,
**kwargs) File "C:\dev\devtools\apps\python3\python-3.9.0\lib\site-packages\requests\sessions.py",
line 587, in request resp = self.send(prep, **send_kwargs) File
"C:\dev\devtools\apps\python3\python-3.9.0\lib\site-packages\requests\sessions.py",
line 701, in send r = adapter.send(request, **kwargs) File
"C:\dev\devtools\apps\python3\python-3.9.0\lib\site-packages\requests\adapters.py",
line 565, in send raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError:
HTTPSConnectionPool(host='chromedriver.storage.googleapis.com',
port=443): Max retries exceeded with url: /LATEST_RELEASE_114.0.5735
(Caused by NewConnectionError('<urllib3.connection.HTTPSConnection
object at 0x0000023F38A7D7C0>: Failed to establish a new connection:
[Errno 11001] getaddrinfo failed'))

Process finished with exit code 1

Any advice appreciated, thank you.


Solution

  • If you're using selenium 4.10.0 or newer, then the driver manager is already included. This code will handle that, and be compatible with any platform:

    import sys
    import time
    from selenium import webdriver
    
    options = webdriver.ChromeOptions()
    options.add_argument("--disable-notifications")
    options.add_argument("--disable-gpu")
    options.add_argument("--disable-dev-shm-usage")
    options.add_argument("--no-sandbox")
    if "linux" in sys.platform:
        options.add_argument("--headless=new")
    options.add_experimental_option(
        "excludeSwitches", ["enable-automation", "enable-logging"],
    )
    driver = webdriver.Chrome(options=options)
    driver.get("https://www.google.com/")
    time.sleep(1)
    driver.quit()