Customize Chrome WebDriver Session with ChromeOptions

I didn’t really notice this thing when I worked on automated web testing in my previous job. But every time I open the browser via webdriver, the existing browser setting is not automatically loaded. It’s as if we open the clean, brand new browser fresh after installation. I wasn’t bothered by this back then since our testing procedure had nothing to do with browser setting or configuration, until I created an automated web script for entirely different purpose.

When I use it for my own personal purpose, I realized that I want (and need) to keep my existing browser settings. That includes extensions and bookmark I heavily use for easier navigation. Otherwise, it would cancel out the benefit of automation I did. Since I use Chrome on daily basis, I wanted to figure out how to load my existing Chrome setting into ChromeDriver’s browser.

Turned out that there is a way. Chromedriver provides some functionalities to customize its session. It’s called ChromeOptions and Capabilities, where we can specify the configurations for our webdriver. You can find the documentations here, but the examples are written only in Java and Ruby. I need to dig into Selenium Python documentation and some inspiration from StackOverflow to help me figuring out how to implement those functions in Python. I haven’t tried everything, and because the docs seems like all over the place, I haven’t gotten full grasp of what every function does.

However, this code worked quite well for me:

from import Options
from selenium.webdriver import DesiredCapabilities
#set web driver
chrome_options = Options()
chrome_options.add_argument('user-data-dir=/Users/your_username/Library/Application Support/Google/Chrome') #Your google chrome setting
capabilities = DesiredCapabilities.CHROME.copy()
chromedriver = '/Volumes/DATA/_PyRandomProjects/autologin/chromedriver/chromedriver' #where you put chromedriver
driver = webdriver.Chrome(executable_path=chromedriver, chrome_options = chrome_options, desired_capabilities=capabilities)

Basically, with .add_argument() function, you can create your own preferred chrome settings each time ChromeDriver starts a new session. For example: maximizing windows, loading profiles, loading only certain extensions, etc. In my case, I loaded my existing Chrome setting by specifying my Chrome user data path.

You can also add multiple arguments to ChromeOptions. For example:

chrome_options.add_argument('user-data-dir=/Users/your_username/Library/Application Support/Google/Chrome')
chrome_options.add_argument('load-extension=/Users/your_username/Library/Application Support/Google/Chrome/Default/Extensions/enbepfpjlejecgbmaijolhgjmpkiimcd/0.5_0

If you want to load only selected Chrome extension, you can do so as follows:


But accessing extension this way requires some practical knowledge about which code belongs to what extension (which you can actually figure out by accessing Chrome://extensions on browser). You have to list each individual extensions and make sure the manifest.json file inside each extension version is available. Maybe there is less painful way to load extensions, but so far the easiest way I know is just use the single existing user data directory.

I know it sounds very technical (?), but part of my purpose of writing this finding is so that my future self don’t have to suffer suppose I forgot the practical details later (lesson learned from past mistake).

Maybe next time I’ll write something more understandable 😀


One thought on “Customize Chrome WebDriver Session with ChromeOptions

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s