import undetected_chromedriver as uc
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
from otp import get_otp
import time
options = webdriver.ChromeOptions()
# options.add_argument("--disable-dev-shm-usage")
# options.add_argument('--enable-javascript')
# options.add_argument('--headless')
# options.add_argument('--no-sandbox')
webdriver_service = Service(ChromeDriverManager().install())
webdriver_service.start()
driver = uc.Chrome(service=webdriver_service, options=options)
there is such a code. After 10 launches, I started getting this error -
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot connect to chrome at 127.0.0.1:54585 from session not created: This version of ChromeDriver only supports Chrome version 121 Current browser version is 120.0.6099.227
Any thoughts pls?
Currently with
undetected-chromedriver, you would need to update theversion_mainarg ofuc.Chrome()every time a new version of Chrome comes out. That's not self-sustaining. Alternatively, there's https://github.com/seleniumbase/SeleniumBase with UC Mode, which is basicallyundetected-chromedriverwith some improvements, such as automatically getting a version of chromedriver that is compatible with your version of Chrome. Setuc=Trueto activate UC Mode in a SeleniumBase script. Here's an example:There's some documentation about it here: https://github.com/seleniumbase/SeleniumBase/issues/2213
Here's a larger script:
For customizing the reconnect time when loading a URL that has bot detection services, you could swap the
get()line with something like this:(The
reconnect_timeis 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.)