pythonselenium-webdriverselenium-chromedriverundetected-chromedriverseleniumbase

Selenium cannot connect to chromedriver


Here is my code. Works perfectly on local machine but in pythonanywhere I am getting this error.

Code:

from seleniumbase import Driver
    driver = Driver(uc=True, headless=True, no_sandbox=True, disable_gpu=True)
    
        try:
            driver.get('https://google.com/')
            print("Page title was '{}'".format(driver.title))
        finally:
            driver.quit()

Error:

Traceback (most recent call last):
  File "/home/mefor/.local/lib/python3.10/site-packages/seleniumbase/core/browser_launcher.py", line 3478, in get_local_driver
    driver = undetected.Chrome(
  File "/home/mefor/.local/lib/python3.10/site-packages/seleniumbase/undetected/__init__.py", line 312, in __init__
    super().__init__(options=options, service=service_)
  File "/home/mefor/.local/lib/python3.10/site-packages/selenium/webdriver/chrome/webdriver.py", line 45, in __init__
    super().__init__(
  File "/home/mefor/.local/lib/python3.10/site-packages/selenium/webdriver/chromium/webdriver.py", line 61, in __init__
    super().__init__(command_executor=executor, options=options)
  File "/home/mefor/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 208, in __init__
    self.start_session(capabilities)
  File "/home/mefor/.local/lib/python3.10/site-packages/seleniumbase/undetected/__init__.py", line 448, in start_session
    super().start_session(capabilities)
  File "/home/mefor/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 292, in start_session
    response = self.execute(Command.NEW_SESSION, caps)["value"]
  File "/home/mefor/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 347, in execute
    self.error_handler.check_response(response)
  File "/home/mefor/.local/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 229, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot connect to chrome at 127.0.0.1:9222
from chrome not reachable
Stacktrace:
#0 0x55bb84c92e89 <unknown>

Before I tried directly undetected_chromedriver it was giving same error too. import undetected_chromedriver as uc

options = uc.ChromeOptions()
options.add_argument("--disable-extensions")
options.add_argument('--disable-application-cache')
options.add_argument('--disable-gpu')
options.add_argument("--no-sandbox")
options.add_argument("--headless")
options.add_argument("--disable-setuid-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--incognito")

driver = uc.Chrome(options=options)
    with driver:
        driver.get('https://google.com')


This code works in pythonanywhere

from sbvirtualdisplay import Display
from seleniumbase import Driver

display = Display(visible=0, size=(1440, 1880))
display.start()

driver = Driver(uc=True, headless=False, incognito=True)
try:
    driver.uc_open_with_reconnect("https://google.com/", 3)
finally:
    driver.quit()

display.stop()

Solution

  • Use SB() instead of Driver() so that you get the built-in virtual display that you need when running on a Linux Server that has no GUI:

    from seleniumbase import SB
    
    with SB(uc=True, xvfb=True) as sb:
        sb.driver.get("https://google.com/")
        print("Page title was '{}'".format(sb.driver.title))
    
    

    (Otherwise you'll need to use https://github.com/mdmintz/sbVirtualDisplay)

    If for some reason xvfb=True didn't work for you, use headless2=True instead. From all the Linux environments I've seen, either xvfb=True OR headless2=True does the trick when using the SB() format.

    If you're sticking with the Driver() format, then know how to use sbvirtualdisplay correctly:

    from sbvirtualdisplay import Display
    from seleniumbase import Driver
    
    display = Display(visible=0, size=(1440, 1880))
    display.start()
    
    driver = Driver(uc=True, headless=False, incognito=True)
    try:
        driver.uc_open_with_reconnect("https://google.com/", 3)
    finally:
        driver.quit()
    
    display.stop()
    

    (That overrides the default headless mode on Linux and forces your script to go through the virtual display instead.)


    Also note that sometimes, WebDriverException: Message: unknown error: cannot connect to chrome at 127.0.0.1:9222 from chrome not reachable means that Chrome wasn't installed on the machine already. If that's the case, make sure that Chrome gets installed first.