Skip to content

Instantly share code, notes, and snippets.

@anandsunderraman
Last active February 21, 2023 01:07
Show Gist options
  • Star 53 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save anandsunderraman/e351485319a8a0e7df7e to your computer and use it in GitHub Desktop.
Save anandsunderraman/e351485319a8a0e7df7e to your computer and use it in GitHub Desktop.
Selenium Web Driver Set Chrome Options
//import the selenium web driver
var webdriver = require('selenium-webdriver');
var chromeCapabilities = webdriver.Capabilities.chrome();
//setting chrome options to start the browser fully maximized
var chromeOptions = {
'args': ['--test-type', '--start-maximized']
};
chromeCapabilities.set('chromeOptions', chromeOptions);
var driver = new webdriver.Builder().withCapabilities(chromeCapabilities).build();
@jacksonsmith
Copy link

Thanks

@ybratun
Copy link

ybratun commented Mar 10, 2019

Thank you!

@EzequielCaballero
Copy link

EzequielCaballero commented Jun 12, 2019

Thanks for the tip!

If it helps and for those to come with the same issue/break, now the value name: chromeOptions is goo:chromeOptions (Selenium-webdriver v4 & Chrome driver v.75)

Here an example changing the default language of the Chrome browser and disabling the notification: Chrome is being controlled by automated test software. Also includes an extra option to define the custom profile to be used (in the browser).

const { Builder, Capabilities } = require("selenium-webdriver");

let userProfilePath = "C:/path_to_your_test_profile";
let chromeCapabilities = Capabilities.chrome();

//Setting chrome options
chromeCapabilities.set("goog:chromeOptions", {
  args: [
      "--lang=en",
      "disable-infobars",
      `user-data-dir=${userProfilePath}`
  ]
});

driver = await new Builder()
            .forBrowser("chrome")
            .withCapabilities(chromeCapabilities)
            .build();

@codeStryke
Copy link

@EzequielCaballero thank you!

@woshahua
Copy link

@EzequielCaballero you just damn awesome, bro

@codingyourlife
Copy link

codingyourlife commented Aug 13, 2019

Took me. while to figure this ChromeOptions in javascript nodejs out.

Reasons:

  • You cannot say import { Options } from "selenium-webdriver/chrome"; - no idea why to be honest
  • Testing it with start-maximized is a bad idea. Didn't work for me. setMobileEmulation or my blink-settings argument work
  • It is really confusing because there are Capabilities, Options, DesiredCapabilities ChromeOptions etc. and you can load Options from capabilities and such strange things

Here my working example where I disable all images through command line argument (pretty obvious if it works):

import webdriver from "selenium-webdriver";
import chrome from "selenium-webdriver/chrome";

export const getNewChromeWrapperDriver = () => {
  // var chromeCapabilities = webdriver.Capabilities.chrome();

  const chromeOptions = new chrome.Options();
  chromeOptions.addArguments("--blink-settings=imagesEnabled=false");

  var driver = new webdriver.Builder()
    .forBrowser("chrome")
    // .withCapabilities(chromeCapabilities)
    .setChromeOptions(chromeOptions)
    .build();

  const seleniumWrapper = {
    webdriver: webdriver,
    driver: driver,
    quit: async () => {
      await driver.quit();
    }
  };

  return seleniumWrapper;
};

As you can see chromeCapabilities are not needed in contrast to some solutions above.

Usage:

import { getNewChromeWrapperDriver } from "./helper/seleniumWrapper.mjs";

const { driver, webdriver } = getNewChromeWrapperDriver()

I kept finding this gist so I thought I add my snippet here...

@jbdoster
Copy link

jbdoster commented Sep 4, 2019

Thank you!!!

@codingyourlife
Copy link

Addition to my comment. I've now upgraded to node 12 since it is now LTS and ran into this issue:
ERR_MODULE_NOT_FOUND selenium-webdriver/chrome

Since import of selenium-webdriver/chrome does no longer work
import chrome from "selenium-webdriver/chrome"; //broken

Here is my workaround:

import require from "requirejs";
const chrome = require("selenium-webdriver/chrome"); //works

This again took me quite a while to find out so I hope this helps!

@harrisonhenri
Copy link

Is it possible to disable w3c using this config?

@rachidelaid
Copy link

dose anyone knows how to do the same with firefox. (open firefox with a specific profile path)

@qquach
Copy link

qquach commented Oct 14, 2020

some good source: https://chromedriver.chromium.org/capabilities
and https://peter.sh/experiments/chromium-command-line-switches/

The new syntax for nodejs (NOTE: the key value)
chromeCapabilities.set("goog:chromeOptions", options);

@ramrami
Copy link

ramrami commented Oct 17, 2020

Thank you

@gulraiz-malhi
Copy link

Thanks @qquach

@jcharnley
Copy link

@purejgleason
Copy link

You rock!

@novwhisky
Copy link

For newer versions of Node with ES modules enabled (14+)

import chrome from 'selenium-webdriver/chrome.js';

let opts = new chrome.Options();
opts.excludeSwitches('enable-automation'); // disable 'Chrome is being controlled by automation' banner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment