小编典典

直接将require('chromedriver).path传递给selenium-webdriver

selenium

tl; dr: 有人知道如何在不设置PATH环境变量的情况下,将chromedriver的路径传递给代码中的selenium-
webdriver吗?

我正在尝试将selenium-webdriver与chrome一起使用,但宁愿不实际安装chromedriver并操纵路径。我有以下代码:

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

在路径中未设置chromedriver的情况下,将引发错误:

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.

我不想设置路径,所以我从npm安装了chromedriver并将其添加到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"
},

现在,我已经安装了chromedriver并可以使用来获取路径require('chromedriver').path,但是我无法将其传递给selenium-
webdriver。有人知道吗


阅读 466

收藏
2020-06-26

共1个答案

小编典典

您需要创建并设置自己的默认Chrome服务。

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();
2020-06-26