How to use User Profile With SeleniumBase?

439 views Asked by At

Code:

from seleniumbase import Driver


driver = Driver(uc=True)
driver.get("https://example.com")
driver.click("a")
p_text = driver.find_element("p").text
print(p_text)

this code works fine but i want to add a user profile but when i try

from seleniumbase import Driver

ud =  r"C:\Users\USER\AppData\Local\Google\Chrome\User Data\Profile 9"

driver = Driver(uc=True, user_data_dir=ud)
driver.get("https://example.com")
driver.click("a")
p_text = driver.find_element("p").text
print(p_text)

this makes a profile called person 1 that works like a normal user and has everything saved but what if i want to access a specific profile?

edit: it goes to the path i give it but appends a \Default so it goes to the default profile of that path so: C:\Users\USER\AppData\Local\Google\Chrome\User Data\Profile 9 would be C:\Users\USER\AppData\Local\Google\Chrome\User Data\Profile 9\Default

Command Line "C:\Program Files\Google\Chrome\Application\chrome.exe" --window-size=1280,840 --disable-dev-shm-usage --disable-application-cache --disable-browser-side-navigation --disable-save-password-bubble --disable-single-click-autofill --allow-file-access-from-files --disable-prompt-on-repost --dns-prefetch-disable --disable-translate --disable-renderer-backgrounding --disable-backgrounding-occluded-windows --disable-features=OptimizationHintsFetching,OptimizationTargetPrediction --disable-popup-blocking --homepage=chrome://new-tab-page/ --remote-debugging-host=127.0.0.1 --remote-debugging-port=53654 --user-data-dir="C:\Users\fun64\AppData\Local\Google\Chrome\User Data\Profile 9" --lang=en-US --no-default-browser-check --no-first-run --no-service-autorun --password-store=basic --log-level=0 --flag-switches-begin --flag-switches-end --origin-trial-disabled-features=WebGPU Executable Path C:\Program Files\Google\Chrome\Application\chrome.exe Profile Path C:\Users\fun64\AppData\Local\Google\Chrome\User Data\Profile 9\Default

1

There are 1 answers

1
Michael Mintz On BEST ANSWER

Partially due to reasons outlined in https://stackoverflow.com/a/67960202/7058266, the best way to handle multiple profiles is to have a unique user-data-dir for each one.

Also, since you're using SeleniumBase UC Mode, for reasons outlined in https://www.youtube.com/watch?v=5dMFI3e85ig at the 21-minute mark, don't mix a non-UC-Mode user-data-dir with a UC Mode user-data-dir, because you could get detected that way.

Use a user-data-dir that gets created by UC Mode:

from seleniumbase import Driver

driver_1 = Driver(uc=True, user_data_dir="my_user_dir_1")
driver_2 = Driver(uc=True, user_data_dir="my_user_dir_2")
# ...
driver_1.quit()
driver_2.quit()

Longer example:

from seleniumbase import Driver

driver = Driver(uc=True, user_data_dir="my_user_dir_1")
try:
    driver.get("https://example.com")
    driver.click("a")
    print(driver.get_text("p"))
finally:
    driver.quit()