Headless automation with Nodejs Selenium Webdriver

后端 未结 4 1661
眼角桃花
眼角桃花 2021-01-30 18:30

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.

相关标签:
4条回答
  • 2021-01-30 18:54

    Try this one:

    var webdriver = require('selenium-webdriver'),
        chrome    = require('selenium-webdriver/chrome')
        By        = webdriver.By,
        until     = webdriver.until,
        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');
    
    0 讨论(0)
  • 2021-01-30 18:59

    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.

    0 讨论(0)
  • 2021-01-30 19:10

    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.

    0 讨论(0)
  • 2021-01-30 19:16

    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().headless().windowSize(screen))
        .setFirefoxOptions(new firefox.Options().headless().windowSize(screen))
        .build();
    

    Headless Chrome available since major version 59.0 APR-2017

    Headless Firefox available since major version 56.0 SEP-2017

    0 讨论(0)
提交回复
热议问题