26

I am working with an automation tool which has to be deployed inside an ubuntu server, my wonder is if is possible to use chrome in a silent way with Selenium Webdriver.

I've tried the following code so far, but it keeps opening the browser (I'm doing the tests in a Windows 10):

    var webdriver = require('selenium-webdriver'),
        chrome    = require('selenium-webdriver/chrome')
        By        = webdriver.By,
        until     = webdriver.until,
        options   = new chrome.Options();
        options.addArguments('--headless');
    var path = require('chromedriver').path;
    var service = new chrome.ServiceBuilder(path).build();
        chrome.setDefaultService(service);
    var driver = new webdriver.Builder().forBrowser('chrome').withCapabilities(options.toCapabilities()).build();

driver.get('https://www.google.com');

Note that the addArguments('--headless') is the parameter that should make the navigation silent, but apparently it's not working or I am missing something I am not aware of.

If there is something I am missing, please tell me because I don't know if what I want to do is possible, as It is the frist time I work with this kind of technology.

Thanks.

1
  • What version of Chrome are you using? --headless works starting with version 59, version 60 on Windows. Commented Jul 27, 2017 at 16:28

4 Answers 4

71

Updated answer circa FEB-2018.

Referencing the Selenium Webdriver NodeJS Examples (commit 5bf50c4)

const chrome = require('selenium-webdriver/chrome');
const firefox = require('selenium-webdriver/firefox');
const {Builder, By, Key, until} = require('selenium-webdriver');

const screen = {
  width: 640,
  height: 480
};

let driver = new Builder()
    .forBrowser('chrome')
    .setChromeOptions(new chrome.Options().addArguments('--headless').windowSize(screen))
    .setFirefoxOptions(new firefox.Options().addArguments('--headless').windowSize(screen))
    .build();

Headless Chrome available since major version 59.0 APR-2017

Headless Firefox available since major version 56.0 SEP-2017

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, that helped. You could just fix the screen object: {width:640,height:480}
13

Try this one:

var webdriver = require('selenium-webdriver'),
    chrome    = require('selenium-webdriver/chrome')
    options   = new chrome.Options();
    options.addArguments('headless'); // note: without dashes
    options.addArguments('disable-gpu')
var path = require('chromedriver').path;
var service = new chrome.ServiceBuilder(path).build();
    chrome.setDefaultService(service);
var driver = new webdriver.Builder()
    .forBrowser('chrome')
    .withCapabilities(webdriver.Capabilities.chrome()) 
    .setChromeOptions(options)                         // note this
    .build();

driver.get('https://www.google.com');

1 Comment

What do By and Until do?
10

Unfortunately the headless() method does not exist any more in Webdriver JS.

Use

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

var driver = new Builder().forBrowser('chrome')
            .setChromeOptions(new chrome.Options().addArguments('--headless'))
            .build();

instead.

2 Comments

Thanks for your comment, for now we stopped using selenium webdriver and moved to puppeteer, a much more reliable solution for our scenario.
@avilac I completed the project in selenium-webdriver and now i found puppeteer was also a better solution oO. I will give it a shot in the next project,
2

To start Chrome in headless mode, simply call Options.headless(). Starting in headless mode currently also disables GPU acceleration. This is the code:

  var seleniumWebdriver = require('selenium-webdriver');
  var chrome    = require('selenium-webdriver/chrome');

  var options   = new chrome.Options().headless();

  var driver = new seleniumWebdriver.Builder()
    .forBrowser('chrome')
    .setChromeOptions(options)
    .build();

Note: For security, Chrome disables downloads by default when in headless mode. You may call setDownloadPath to re-enable downloads.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.