3

im using nodejs version 8.11.3 and npm version 6.9.0 along with chrome version 75.0.3770.90 (the latest). Im trying to use selenium to open a headless chrome. But the issue is that no matter what I try it just doesnt work. Im using Windows 10 version 1903 (Build 18362.175). Ive installed selenium (npm i selenium-webdriver) and correctly configured them in my windows PATH. There has not been any other preperation for selenium. I tried using all sorts of code that I found online from different sources to get the headless mode running. One example that I tried:

 let chrome = require('selenium-webdriver/chrome');
 let {Builder} = require('selenium-webdriver');

 let driver = new Builder()
     .forBrowser('chrome')
     .setChromeOptions(new chrome.Options().headless())
     .build();

which can be found here: https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/chrome.html

or this code:

const fs = require('fs');
const webdriver = require('selenium-webdriver');
const chromedriver = require('chromedriver');

const chromeCapabilities = webdriver.Capabilities.chrome();
chromeCapabilities.set('chromeOptions', {args: ['--headless']});

const driver = new webdriver.Builder()
  .forBrowser('chrome')
  .withCapabilities(chromeCapabilities)
  .build();

// Navigate to google.com, enter a search.
driver.get('https://www.google.com/');
driver.findElement({name: 'q'}).sendKeys('webdriver');
driver.findElement({name: 'btnG'}).click();
driver.wait(webdriver.until.titleIs('webdriver - Google Search'), 1000);

Which can be found here: https://developers.google.com/web/updates/2017/04/headless-chrome#drivers

And Ive also tried using different code snippets from stackoverflow threads that I found. Now I intentionally copy pasted these code snippets, not because I cant write it myself but because I wanted to use code that should 100% work (since its from official sources) to eliminate that the issue might be my own issue. What happens with that code is that it just starts as normal. All of selenium is working fine but its just not running headless. Im also not getting an error in the console.

Now the question is, did I miss something that I should have done? Do I need to install something thats so obvious that nobody mentions it? Is there any way to troubleshoot the issue? Thanks for all replies!

EmotionIce
  • 41
  • 1
  • 2
  • 3

1 Answers1

2

Try this solution I have my selenium run in headless chrome in CI Gitlab. I hope this will help

 driver = await new Builder()
    .forBrowser('chrome')
    .setChromeOptions(new chrome.Options().addArguments(['--headless','--no-sandbox', '--disable-dev-shm-usage']))
    .build();

I have also added these in my scripts to run:

"confidence-check": "wdio wdio.conf.js",
"automate-testing": "npm run confidence-check --host=selenium__standalone-chrome",
"automate-ci": "start-server-and-test start http://localhost:3000 automate-testing"

You just need to set your wdio.conf.js file in the root directory

Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41
shaurya
  • 581
  • 1
  • 4
  • 8
  • Your addArguments solved a similar problem for me on Ubuntu 18.0.4 when I was getting error message: `WebDriverError: unknown error: Chrome failed to start: exited abnormally. (unknown error: DevToolsActivePort file doesn't exist)` – phatfingers Apr 28 '22 at 17:54