In this Blog we take a look at some of the options provided with the ChromeDriver Server for starting Chrome with various options when using Selenium WebDriver. The examples are in C# so you may need to tailor for Java.

Set Path to the ChromeDriver Server

System.setProperty("webdriver.chrome.driver","C:\\Drivers\\chromedriver.exe")

WebDriver driver = new ChromeDriver();

driver.get("http://www.google.com");

or you could add the path to chromedriver.exe in your system path.

Hide the automation toolbar warning

ChromeOptions options = new ChromeOptions();

options.addArgument("disable-infobars");

WebDriver driver = new ChromeDriver(options);

Put Chrome into mobile emulation mode

C# example:

ChromeOptions options = new ChromeOptions();

options.EnableMobileEmulation("Google Nexus 5");

IWebDriver driver = new ChromeDriver(options);

driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);

Java Example:

Map<String, String>; mobileEmulation = new HashMap<String, String>();

mobileEmulation.put("deviceName", "Google Nexus 5");

Map<String, Object> chromeOptions = new HashMap<String, Object>();

chromeOptions.put("mobileEmulation", mobileEmulation);

DesiredCapabilities capabilities = DesiredCapabilities.chrome();

capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);

WebDriver driver = new ChromeDriver(capabilities);

An alternative is to set individual device attributes, this is a more complex approach, and details can be found at https://sites.google.com/a/chromium.org/chromedriver/mobile-emulation

Use custom profile (also called user data directory)

By default, ChromeDriver will create a new temporary profile for each session. At times you may want to set special preferences or just use a custom profile altogether. If the former, you can use the ‘chrome.prefs’ capability (described later below) to specify preferences that will be applied after Chrome starts. If the latter, you can use the user-data-dir Chrome command-line switch to tell Chrome which profile to use:

ChromeOptions options = new ChromeOptions();

options.addArgument("user-data-dir=/path/to/your/custom/profile");

You can create your own custom profile by just running Chrome (on the command-line or through ChromeDriver) with the user-data-dir switch set to some new directory. If the path doesn’t exist, Chrome will create a new profile in the specified location. You can then modify the profile settings as desired, and ChromeDriver can use the profile in the future. Open chrome://version in the browser to see what profile Chrome is using.

Start Chrome maximized

ChromeOptions options = new ChromeOptions();

options.addArgument("start-maximized");

Using a Chrome executable in a non-standard location

ChromeOptions options = new ChromeOptions();

options.setBinary("/path/to/other/chrome/binary");

Using Headless Chrome

ChromeOptions options = new ChromeOptions();

options.addArgument("headless");

driver = new ChromeDriver(options);

Add Extensions to Chrome

ChromeOptions options = new ChromeOptions();

options.AddExtension(@"c:\\path\to\extension.crx");

IWebDriver driver = new ChromeDriver(options);

Help

Run chromedriver --help to see command line arguments for your version.

NOTE: The above examples are in WebDriver C#, for Java, you may need to use options.addArguments

  

Selenium Training Courses