pythonselenium-webdriverundetected-chromedriver

Selenium undetected chromedriver with different chrome-versions?


I have the following code which works fine on a computer where chrome 122 is installed currently -

import undetected_chromedriver as uc
driver = uc.Chrome()
driver.get('https://ballzy.eu/en/men/sport/shoes') 

But when I run this code on a computer where a different chrome-version is installed like 120, I get the following error -

(selenium) C:\DEV\Fiverr\ORDER\stefamn_jan669_jankore_janxx2\Ballzy>python test3.py
Traceback (most recent call last):
  File "C:\DEV\Fiverr\ORDER\stefamn_jan669_jankore_janxx2\Ballzy\test3.py", line 2, in <module>
    driver = uc.Chrome(version_main=122)
  File "C:\DEV\.venv\selenium\lib\site-packages\undetected_chromedriver\__init__.py", line 466, in __init__
    super(Chrome, self).__init__(
  File "C:\DEV\.venv\selenium\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 45, in __init__
    super().__init__(
  File "C:\DEV\.venv\selenium\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 61, in __init__
    super().__init__(command_executor=executor, options=options)
  File "C:\DEV\.venv\selenium\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 208, in __init__
    self.start_session(capabilities)
  File "C:\DEV\.venv\selenium\lib\site-packages\undetected_chromedriver\__init__.py", line 724, in start_session
    super(selenium.webdriver.chrome.webdriver.WebDriver, self).start_session(
  File "C:\DEV\.venv\selenium\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 292, in start_session
    response = self.execute(Command.NEW_SESSION, caps)["value"]
  File "C:\DEV\.venv\selenium\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 347, in execute
    self.error_handler.check_response(response)
  File "C:\DEV\.venv\selenium\lib\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:50596
from session not created: This version of ChromeDriver only supports Chrome version 122
Current browser version is 120.0.6099.200

Is it somehow possible that the correct chromedriver is automatically downloaded?

(when I use the normal selenium driver - I just use the following driver-definition, and it works fine with that on several computers)

srv=Service()
  driver = webdriver.Chrome (service=srv, options=options) 

How can i do this also with the undetected chromedriver so it is working on differnt chrome version installations on different computers?


Solution

  • With undetected-chromedriver, you have to update the version_main arg of uc.Chrome() if you don't want to use the latest available driver version to match Chrome. Eg:

    import undetected_chromedriver as uc
    
    driver = uc.Chrome(version_main=120)
    

    Alternatively, you can use https://github.com/seleniumbase/SeleniumBase with UC Mode, which is basically undetected-chromedriver with some improvements, such as automatically getting a version of chromedriver that is compatible with your version of Chrome. Set uc=True to activate UC Mode in a SeleniumBase script. Here's an example:

    from seleniumbase import Driver
    
    driver = Driver(uc=True)
    

    There's some documentation about it here: https://github.com/seleniumbase/SeleniumBase/issues/2213

    Here's a larger script:

    from seleniumbase import Driver
    
    driver = Driver(uc=True)
    driver.get("https://nowsecure.nl/#relax")
    # DO MORE STUFF
    driver.quit()
    

    For customizing the reconnect time when loading a URL that has bot detection services, you could swap the get() line with something like this:

    driver.uc_open_with_reconnect("https://nowsecure.nl/#relax", reconnect_time=5)
    

    (The reconnect_time is the wait time before chromedriver reconnects with Chrome. Before the time is up, a website cannot detect Selenium, but it also means that Selenium can't yet issue commands to Chrome.)