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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27733731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 07:54:53  来源:igfitidea点击:

Passing require('chromedriver).path directly to selenium-webdriver

javascriptnode.jsseleniumselenium-chromedriver

提问by jt000

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

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

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:

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

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:

如果没有在路径中设置 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.

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

我不想设置我的路径,所以我从 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"
},

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?

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

回答by jt000

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

您需要创建和设置您自己的默认 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();

回答by mucsi96

You can also do this:

你也可以这样做:

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

const driver = new webdriver.Builder()
    .withCapabilities(webdriver.Capabilities.chrome())
    .build();

回答by irlatech

Option 1:
process.env.PATH = 'path to chrome driver binary folder';
var driver = new Builder().forBrowser('chrome').build();

Option 2:
install chromedriver (npm install chromedriver)
require('chromedriver'); in your code

//chrome driver will automatically look for chromedriver or chromedriver.exe in the path mentioned based on OS
reference: 
https://github.com/SeleniumHQ/selenium/blob/master/javascript/node/selenium-webdriver/chrome.js line 142
https://github.com/SeleniumHQ/selenium/blob/master/javascript/node/selenium-webdriver/io/index.js line 237