13

After using Selenium, I decided to try undetected-chromedriver so I installed it using

 pip install undetected-chromedriver

However, running this simple script

import undetected_chromedriver.v2 as uc

options = uc.ChromeOptions()
options.add_argument('--no-sandbox')

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

gives the error

selenium.common.exceptions.WebDriverException: Message: unknown error: cannot connect to chrome at 127.0.0.1:37541 from chrome not reachable

There's no issue using regular Selenium

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument("--no-sandbox"); # Bypass OS security model

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

Here's the traceback

Traceback (most recent call last):
  File "/root/test-bot/src/test.py", line 6, in <module>
    driver = uc.Chrome()
  File "/root/anaconda3/envs/test/lib/python3.9/site-packages/undetected_chromedriver/v2.py", line 302, in __init__
    super(Chrome, self).__init__(
  File "/root/anaconda3/envs/test/lib/python3.9/site-packages/selenium/webdriver/chrome/webdriver.py", line 69, in __init__
    super(WebDriver, self).__init__(DesiredCapabilities.CHROME['browserName'], "goog",
  File "/root/anaconda3/envs/test/lib/python3.9/site-packages/selenium/webdriver/chromium/webdriver.py", line 93, in __init__
    RemoteWebDriver.__init__(
  File "/root/anaconda3/envs/test/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 248, in __init__
    self.start_session(capabilities, browser_profile)
  File "/root/anaconda3/envs/test/lib/python3.9/site-packages/undetected_chromedriver/v2.py", line 577, in start_session
    super(Chrome, self).start_session(capabilities, browser_profile)
  File "/root/anaconda3/envs/test/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 339, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/root/anaconda3/envs/test/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 400, in execute
    self.error_handler.check_response(response)
  File "/root/anaconda3/envs/test/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 236, 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:52681
from chrome not reachable
  (Driver info: chromedriver=2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706),platform=Linux 5.11.0-18-generic x86_64)

Any suggestions?


Attempt #1

Also tried setting the executable_path to /usr/bin/chromedriver

import undetected_chromedriver.v2 as uc

options = uc.ChromeOptions()
options.add_argument('--no-first-run --no-service-autorun --password-store=basic')

CHROME_DRIVER_PATH = '/usr/bin/chromedriver'
driver = uc.Chrome(executable_path=CHROME_DRIVER_PATH, options=options)
with driver:
    driver.get('https://google.com') 

which gives the same error

selenium.common.exceptions.WebDriverException: Message: unknown error: cannot connect to chrome at 127.0.0.1:42305 from chrome not reachable

Checked that the path does exists

# ll /usr/bin/chromedriver
-rwxr-xr-x 1 root root 8298464 Oct  1 14:19 /usr/bin/chromedriver*

Attempt #2

Tried using Xvfb and disabling headless mode

import undetected_chromedriver.v2 as uc
from xvfbwrapper import Xvfb
vdisplay = Xvfb(width=800, height=1280)
vdisplay.start()

options = uc.ChromeOptions()
options.add_argument('--no-first-run --no-service-autorun --password-store=basic')
options.user_data_dir = f'./tmp/test_undetected_chromedriver'
options.add_argument(f'--disable-gpu')
options.add_argument(f'--no-sandbox')
options.add_argument(f'--disable-dev-shm-usage')

CHROME_DRIVER_PATH = '/usr/bin/chromedriver'
driver = uc.Chrome(executable_path=CHROME_DRIVER_PATH, options=options, headless=False)
with driver:
    driver.get('https://google.com') 
    print(driver.title)

slightly different error

selenium.common.exceptions.WebDriverException: Message: unknown error: cannot connect to chrome at 127.0.0.1:42467 from unknown error: unable to discover open pages (Driver info: chromedriver=2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706),platform=Linux 5.11.0-18-generic x86_64)

Athena Wisdom
  • 6,101
  • 9
  • 36
  • 60
  • Try to [set up logging](https://chromedriver.chromium.org/logging) for your driver and find a clue there. – Alexey R. Oct 01 '21 at 15:42

3 Answers3

8

I had the exact same issue in Centos 7 and the problem was the versions of chrome, undetected-chrome and chromedriver where NOT aligned.

Solution that worked for me (I'm using Python v3.8):

  1. Remove the existing versions
cd /YOUR_CHROMEDRIVER_PATH
yum remove google-chrome*
python3.8 -m pip uninstall undetected-chromedriver
rm -f chromedriver
  1. Install aligned versions
python3.8 -m pip install undetected-chromedriver==3.0.6
wget -N http://chromedriver.storage.googleapis.com/96.0.4664.35/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
yum -y install https://dl.google.com/linux/chrome/rpm/stable/x86_64/google-chrome-stable-96.0.4664.110-1.x86_64.rpm
ln -s /usr/bin/google-chrome-stable /bin/chrome
  1. Example of my working python script:

import undetected_chromedriver as uc
from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--enable-javascript')
chrome_options.add_argument('--disable-gpu')
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.0 Safari/605.1.15'
chrome_options.add_argument('User-Agent={0}'.format(user_agent))
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option('useAutomationExtension', True)

driver = uc.Chrome(executable_path='/path_to_your_driver/chromedriver',chrome_options=chrome_options,service_args=['--quiet'])
driver.implicitly_wait(6.5)

driver.get("https://google.com")

Hope this helps someone, as I wasted several days to find this solution!! :-)

Marc
  • 89
  • 1
  • 3
  • I'm not sure why you need to install chromedriver when undetected downloads and patches chromedriver for you. However, even after doing that, this didn't work for me. Granted, I do have Google Chrome 108.0.5359.98 installed but according to your link, the closest matching chromedriver is only 108.0.5359.71. – Cerin Dec 13 '22 at 17:47
  • Thanks for the hint. How did you work out which version of chromedriver and chrome itself you need? I'm trying to run it on Mac Os and I'm getting the same errors. – Babak Tourani Jan 30 '23 at 15:26
1

remove all the things in the profile path.it works for me

buddhi kavindra
  • 126
  • 1
  • 1
  • 11
0

I solved it through manually run chrome.exe --user-data-dir="userdata folder path".
see this github link

hrdom
  • 83
  • 1
  • 7