Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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();
@fatihacet

This comment has been minimized.

Copy link

fatihacet commented Jun 27, 2015

I was looking for this for 4+ hours. No further documentation, article, blog post about how to pass args to chromeOptions or even how to define chromeOptions manually. Thanks for this Gist.

@tylerhjones

This comment has been minimized.

Copy link

tylerhjones commented Apr 15, 2016

Thank you for this

@gonzazoid

This comment has been minimized.

Copy link

gonzazoid commented Apr 28, 2016

you saved me a few hours of my life. Thank you ever so mach!

@johngroves

This comment has been minimized.

Copy link

johngroves commented Jan 10, 2017

👍

@raghuAtWings

This comment has been minimized.

Copy link

raghuAtWings commented Apr 26, 2017

below is my code, it still doesn't open chrome in maximized view. Please help. I am new to this and have been trying to find a solution for hours.

const assert = require('assert');
promise.USE_PROMISE_MANAGER = false;

var webdriver = require('selenium-webdriver');
require('selenium-webdriver/chrome');
require('chromedriver');
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);

describe('test application flow', function() {
  this.timeout(10000);
  let driver;

  beforeEach(async function() {
    driver = new webdriver.Builder()
             .withCapabilities(chromeCapabilities)
             .build();
  });

  afterEach(async function() {
    await driver.wait(() => {})
  });

  it('create application', async function() {
    await driver.get('https://localhost:8000/#/login');
  });

});
@insatmc

This comment has been minimized.

Copy link

insatmc commented Aug 2, 2017

Same problem as @raghuAtWings, any help?

@wenJanus

This comment has been minimized.

Copy link

wenJanus commented Oct 30, 2017

who can tell me how to set chromeOptions with proxy(socks)?
Thanks!

@Flolagale

This comment has been minimized.

Copy link

Flolagale commented Nov 13, 2017

Thanks for this! Saved me hours, worked like a charm.

@keithpotter21

This comment has been minimized.

Copy link

keithpotter21 commented May 18, 2018

if anyone is interested, here's how I changed the download directory...

https://gist.github.com/keithpotter21/c54b734267c507d3b94c3d6373dc0657

many thanks to @anandsunderraman for the init options setup!!!

@aljorhythm

This comment has been minimized.

Copy link

aljorhythm commented May 31, 2018

thanks

@benfr1

This comment has been minimized.

Copy link

benfr1 commented Aug 9, 2018

Hello !

I resolved the issue using this bit of code :

`
var selenium = require('selenium-webdriver');

var capabilities = selenium.Capabilities.chrome();

capabilities.set('chromeOptions',{

'args': ['--headless', '--no-sandbox', 'window-size=1024,768' , '--disable-gpu']

})

this.driver = new selenium.Builder().forBrowser("chrome").withCapabilities(capabilities).build();

`

@lazysergey

This comment has been minimized.

Copy link

lazysergey commented Aug 12, 2018

here's my setup works like a charm

additional info here:
https://github.com/SeleniumHQ/selenium/blob/master/javascript/node/selenium-webdriver/example/
https://sites.google.com/a/chromium.org/chromedriver/mobile-emulation

const { Builder, By, Key, until, Capabilities } = require('selenium-webdriver');
var connect = require('connect');
var serveStatic = require('serve-static');

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

__dirname = "/Users/username/projects/1/Resources/HTML"
connect().use(serveStatic(__dirname)).listen(8888, runSeleniumDriver);

function runSeleniumDriver(server) {
    console.log("running selenium");

    let driver = new Builder()
        // .forBrowser('safari')
        .forBrowser('chrome')
        .setChromeOptions(new Options().setMobileEmulation({ deviceName: 'iPhone 5' }))
        .build();
    driver.get("http://localhost:8888/index.html");

    try {
        driver.findElement(By.css('li:nth-child(2) span')).click();
        driver.findElement(By.css('li:nth-child(9) span')).click();
        driver.findElement(By.css('li:nth-child(5) span')).click();
        driver.wait(until.titleIs('Test'), 30000);
    } catch (err) {

    } finally {
        driver.close();
        driver.quit();
    }
}
@3jacksonsmith

This comment has been minimized.

Copy link

3jacksonsmith commented Nov 7, 2018

Thanks

@ybratun

This comment has been minimized.

Copy link

ybratun commented Mar 10, 2019

Thank you!

@EzequielCaballero

This comment has been minimized.

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

This comment has been minimized.

Copy link

codeStryke commented Jun 25, 2019

@EzequielCaballero thank you!

@woshahua

This comment has been minimized.

Copy link

woshahua commented Jun 25, 2019

@EzequielCaballero you just damn awesome, bro

@codingyourlife

This comment has been minimized.

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

This comment has been minimized.

Copy link

jbdoster commented Sep 4, 2019

Thank you!!!

@codingyourlife

This comment has been minimized.

Copy link

codingyourlife commented Oct 26, 2019

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.