ultrafunkamsterdam / undetected-chromedriver

Custom Selenium Chromedriver | Zero-Config | Passes ALL bot mitigation systems (like Distil / Imperva/ Datadadome / CloudFlare IUAM)
https://github.com/UltrafunkAmsterdam/undetected-chromedriver
GNU General Public License v3.0
9.9k stars 1.16k forks source link

Maintaining a login session onsite #322

Open ENUM1 opened 3 years ago

ENUM1 commented 3 years ago

Apologies if this isn't specific to UC, but for my project, it requires me to log in to a website and perform an action - however, these are repetitive tasks and currently for each one I am launching a new driver as they aren't quite concurrent.

I'm wondering what the best practices might be for maintaining a session once logged in, or perhaps changing to login periodically, etc.

All help is appreciated.

frederikme commented 3 years ago

You can store a session (cookies, logged in, etc) in a directory and load it the next time you run the script. So when you run your script again, you start wherever you left off with the same cookies and sessions. For example you'll be logged in automatically to a site you previously logged in to when getting the website url browser.get(TARGET_URL).

An example of how to create/load such a profile can be found below.

chrome_profile_dir = './chrome_profile_1'
options = uc.ChromeOptions()

# Create empty profile to start with if it's not yet used
if not os.path.isdir(chrome_profile_dir):
    os.mkdir(chrome_profile_dir)

Path(f'{chrome_profile_dir}/First Run').touch()
options.add_argument(f'--user-data-dir={chrome_profile_dir}/')

# Start driver/browser with the given user profile / cookies loaded
browser = uc.Chrome(options=options)