Passing require('chromedriver).path directly t

2019-02-04 15:41发布

tl;dr: Does anyone know how to pass the path of chromedriver to selenium-webdriver in code without setting the PATH environment variable?

I'm attempting to use selenium-webdriver with chrome, but would prefer to not physically install chromedriver and manipulate the path. I have the following code:

var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().
    withCapabilities(webdriver.Capabilities.chrome()).
    build();

Without chromedriver set in the path, this throws the error:

Error: The ChromeDriver could not be found on the current PATH. Please download the latest 
version of the ChromeDriver from http://chromedriver.storage.googleapis.com/index.html and 
ensure it can be found on your PATH.

I'd prefer not have to setup my path, so I've installed chromedriver from npm and added to my package.json:

"scripts": {
    "preinstall-chromedriver": "npm install",
    "install-chromedriver": "node node_modules/chromedriver/install.js",
    "pretest_e2e": "npm run install-chromedriver",
    "test_e2e": "node release/test/rune2e.js"
},

Now I have chromedriver installed and can get the path with require('chromedriver').path, but I have no way of passing this to the selenium-webdriver. Anyone know?

2条回答
我命由我不由天
2楼-- · 2019-02-04 16:16

You can also do this:

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

const driver = new webdriver.Builder()
    .withCapabilities(webdriver.Capabilities.chrome())
    .build();
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-02-04 16:18

You need to create & set your own default chrome service.

var webdriver = require('selenium-webdriver');
var chrome = require('selenium-webdriver/chrome');
var path = require('chromedriver').path;

var service = new chrome.ServiceBuilder(path).build();
chrome.setDefaultService(service);

var driver = new webdriver.Builder()
    .withCapabilities(webdriver.Capabilities.chrome())
    .build();
查看更多
登录 后发表回答