Firstly, I would ask you to use the undetected_chromedriver module
I have found some issues with the original repo so I have forked a repo that supports user_data_dir and also allows you to provide custom chromedriver path as well.
Uninstall the old module and install this by using git clone and then go the folder and run python setup.py install
Forked repo link : https://github.com/anilabhadatta/undetected-chromedriver
Import the latest undetected_chromdriver module:
import undetected_chromedriver.v2 as ucdriver
For using user_data_dir feature write:
options.user_data_dir = "path_to _user-data-dir"
instead of using this
options.add_argument(f"user_data_dir={path_to _user-data-dir}")
profile_directory name is the same as how we write in selenium
options.add_argument(f"--profile-directory=Default")
For using custom chrome path,
options.binary_location = chrome_path
For using custom chromedriver path,
driver = ucdriver.Chrome(executable_path=f'{path_to_chromedriver}', options=options)
Recently Google made some changes for which the authentication part didn't work.
I have tested this in Python 3.9.0, there are reports that it may not work correctly in 3.10.0
And this is tested in both Windows and Linux.
Final Code:
def load_chrome_driver(headless):
chrome_loc = "/home/ubuntu/Downloads/chromium-browser/"
chrome_path = chrome_loc + "chrome"
chromedriver_path = chrome_loc + "chromedriver"
user_data_dir = "/home/ubuntu/.config/chromium/custom_user"
options = webdriver.ChromeOptions()
if headless:
options.add_argument('headless')
options.add_argument('--profile-directory=Default')
options.add_argument("--start-maximized")
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
options.add_argument("--disable-dev-shm-usage")
options.add_argument('--log-level=3')
options.binary_location = chrome_path
options.user_data_dir = user_data_dir
driver = ucdriver.Chrome(
executable_path=chromedriver_path, options=options)
driver.set_window_size(1920, 1080)
driver.set_window_position(0, 0)
return driver