WebDriverJS: Chrome profile


Motivation

WebDriverJS, by default, initializes browsers without any profile data. This is to ensure a pristine state.

But did you know, there’s a way to initialize a driver with a browser profile. In this tutorial, we’ll learn how to do that with Chrome.

Prerequisites

You need the following installed:

If you’re on Mac OS and have homebrew installed (see video if you don’t have it installed), you can do the following:

# install Chrome browser
$ brew cask install google-chrome
# install ChromeDriver
$ brew cask install chromedriver

These steps are covered in this video.

Build driver

To build your driver:

const { Builder } = require('selenium-webdriver');

const builder = new Builder();
builder.forBrowser('chrome');
const driver = builder.build();

Check out article WebDriverJS: Launch a browser for more details.

Chrome options

Before initializing the driver, add argument user-data-dir or --user-data-dir to Chrome options:

const { Options } = require('selenium-webdriver/chrome');

const options = new Options();
// replace `./path/to/profile` with your Chrome profile path
options.addArguments('user-data-dir=./path/to/profile');

Note: ChromeDriver expects ./path/to/profile/Default/ to exist. If it doesn’t exist, it’ll create the Default/ directory for your profile path.

Set the Chrome options to your builder before building the driver:

// ...
builder.setChromeOptions(options);
const driver = builder.build();

You can verify the profile path is correct by opening chrome://version in your WebDriver browser:

driver.get('chrome://version');

Addendum

You can find all code examples (as well as others), in my webdriverjs-recipes repository.



If you enjoyed this post, please consider supporting this site!