Home > Blockchain >  Add options to selenium chrome browser using nodejs
Add options to selenium chrome browser using nodejs

Time:07-03

I am using selenium with NodeJs 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`

CodePudding user response:

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...
  • Related