use a local chrome profile for automation scripts
When automating tasks with python scripts, you can hit a wall with SSO and oauth2. Creating a local chrome profile in your XDG data directory can allow one SSO login per work day and your python scripts can re-use the sessions.
tldr
Set your chrome options user-data-dir and profile-directory to a local directory under your control to persist SSO/sessions across script runs.
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
home_dir = os.getenv('HOME')
profile_dir = home_dir + '/.local/share/chrome/profile'
chrome_options = Options()
chrome_options.add_argument(r'user-data-dir=' + profile_dir)
chrome_options.add_argument(r'profile-directory=Automation')
driver = webdriver.Chrome(options=chrome_options)
# do some driver/DOM automation
driver.close()
A simple view of chrome profiles
The profile path is where chrome saves passwords, sessions, history, etc.
Building a profile for autmation scripts
We can build a chrome profile for chromedriver and selenium to (re-)use generated session tokens, etc.
import os
from selenium.webdriver.chrome.options import Options
home_dir = os.getenv('HOME')
profile_dir = home_dir + '/.local/share/chrome/profile'
chrome_options = Options()
chrome_options.add_argument(r'user-data-dir=' + profile_dir)
chrome_options.add_argument(r'profile-directory=Automation')
This setups a profile directory at ./local/share/chrome/profile in our home directory. Ideally, you can make this directory more dynamic based on your needs. Check out XDG Base Directory Specs for some cross-platform inspiration.
Using our profile with our chromedriver
from selenium import webdriver
driver = webdriver.Chrome(options=chrome_options)
# do some driver/DOM automation
driver.close()
Why can’t we use my normal chrome profile
Unfortunately, as far as I can tell, two chrome processes can’t use the same profile at the same time. Furthermore, having chromedriver/selenium use your regular chrome profile can cause problems. I made one of my profiles unusuable trying to do this.
Debugging Notes
If SSO/Sessions do not seem to persist across python script runs. A simple way
to check that the profile path is correct is to not close the driver
automatically. And visit chrome://version
in the spawned chrome window.