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

Passing require(#39;chromedriver).path directly to selenium-webdriver(将 require(chromedriver).path 直接传递给 selenium-webdriver)
本文介绍了将 require('chromedriver).path 直接传递给 selenium-webdriver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

我正在尝试将 selenium-webdriver 与 chrome 一起使用,但不希望物理安装 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

SCRIPT5: Access is denied in IE9 on xmlhttprequest(SCRIPT5:在 IE9 中对 xmlhttprequest 的访问被拒绝)
XMLHttpRequest module not defined/found(XMLHttpRequest 模块未定义/未找到)
Show a progress bar for downloading files using XHR2/AJAX(显示使用 XHR2/AJAX 下载文件的进度条)
How can I open a JSON file in JavaScript without jQuery?(如何在没有 jQuery 的情况下在 JavaScript 中打开 JSON 文件?)
quot;Origin null is not allowed by Access-Control-Allow-Originquot; in Chrome. Why?(“Access-Control-Allow-Origin 不允许 Origin null在铬.为什么?)
How to get response url in XMLHttpRequest?(如何在 XMLHttpRequest 中获取响应 url?)