Add options to selenium chrome browser using nodejs

Issue

I am using selenium with Node.js in this way

import {Builder, Browser, By, Key, until} from "selenium-webdriver";
let driver = await new Builder().forBrowser(Browser.CHROME).build();

I want to add chrome options in this way

  const chrome = require('selenium-webdriver/chrome')
  const options = new chrome.Options()

  options.addArguments('--disable-dev-shm-usage')
  options.addArguments('--no-sandbox')
  options.addArguments('--headless')

  let driver = await new    Builder().forBrowser(Browser.CHROME).setChromeOptions(options).build();

But I can only use imports in my project, I can’t use require. I get the following error because of this line

const chrome = require('selenium-webdriver/chrome')

require is not defined in ES module scope, you can use import instead

How can I import chrome instead of require it to add the option? I am importing a lot of module in my code, It will be really difficult to change them all to requires

Solution

You can refer to the file as "selenium-webdriver/chrome.js". I.e.:

import {Options} from "selenium-webdriver/chrome.js";
const options = new Options();

// use options as you always would...

Answered By – Mureinik

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published