Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add options to selenium chrome browser using nodejs

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading