将require('chromedriver).path直接传递给selenium-webdriver [英] Passing require('chromedriver).path directly to selenium-webdriver
问题描述
tl; dr: 有没有人知道如何在不设置PATH环境变量的情况下将chromedriver的路径传递给代码中的selenium-webdriver?
我正在尝试使用带有chrome的selenium-webdriver,但更愿意不在物理上安装chromedriver并操纵路径。我有以下代码:
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();
如果路径中没有设置chromedriver,则会抛出错误:
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.
我不想设置我的路径,所以我从npm安装了chromedriver并添加了到我的package.json:
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"
},
现在我安装了chromedriver并且可以获得路径使用 require('chromedriver')。path
,但我无法将其传递给selenium-webdriver。有人知道吗?
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?
推荐答案
你需要创建&设置您自己的默认Chrome服务。
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();
这篇关于将require('chromedriver).path直接传递给selenium-webdriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!