I want to open an instance of undetected_chromedriver with a pre-set Chrome profile (basically the same thing as this thread asks about but with undetected_chromedriver instead of selenium).
This code works for me, using selenium (the first bit is just cloning the Chrome profile directory into the project directory to avoid an error, based on this thread):
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import undetected_chromedriver as uc
import os
import shutil
# copy Chrome 'Profile 2' folder into this python project folder (for some reason, this is the
# only way Selenium is able to open a chrome browser with a pre-set profile without Chrome getting
# upset about multiple uses of the data directory)
# first, delete the extra files that Chrome automatically adds to user data directories (needed
# if script has already been run)
dir = '{project directory}'
for files in os.listdir(dir):
path = os.path.join(dir, files)
if path not in [{files I want to keep in the project directory}]:
try:
shutil.rmtree(path)
except OSError:
os.remove(path)
# then copy chrome profile into project directory
src = 'C:/Users/{CHROME_USER_DIR}/AppData/Local/Google/Chrome/User Data/Profile 2'
dst = '{project directory}/User Data/Profile 2'
shutil.copytree(src, dst, symlinks=False, ignore=None, ignore_dangling_symlinks=False, dirs_exist_ok=False)
userdatadir = '{project directory}/User Data'
# initialize Chrome instance using Profile 2
options = webdriver.ChromeOptions()
options.add_argument(f"--user-data-dir={userdatadir}")
options.add_argument("profile-directory=Profile 2")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.get('https://www.nyt.com/')
BUT, when I try to do the same thing using undetected_chromedriver:
uc_options = uc.ChromeOptions()
uc_options.add_argument(f"--user-data-dir={userdatadir}")
uc_options.add_argument("profile-directory=Profile 2")
driver = uc.Chrome(service=Service(ChromeDriverManager().install()), options=uc_options, use_subprocess=True)
Chrome errors out as so (see screenshot below), and the Chrome instance is not logged into the specified profile!
Thank you in advance for any help!