使用Nodejs Selenium Webdriver实现无头自动化 [英] Headless automation with Nodejs Selenium Webdriver

查看:470
本文介绍了使用Nodejs Selenium Webdriver实现无头自动化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用必须部署在ubuntu服务器内部的自动化工具,我想知道是否可以通过Selenium Webdriver以静默方式使用chrome.

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.

到目前为止,我已经尝试了以下代码,但是它一直在打开浏览器(我正在Windows 10中进行测试):

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');

请注意,addArguments('-headless')是应该使导航保持静默的参数,但显然它不起作用,或者我缺少我不知道的东西.

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.

谢谢.

推荐答案

大约在2018年2月更新了答案.

Updated answer circa FEB-2018.

引用 Selenium Webdriver NodeJS示例(提交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();

无头Chrome自主要版本59.0 于2017年4月发布

Headless Chrome available since major version 59.0 APR-2017

无头Firefox自主要版本56.0 SEP-2017

Headless Firefox available since major version 56.0 SEP-2017

这篇关于使用Nodejs Selenium Webdriver实现无头自动化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆