Javascript Selenium Nodejs CHROMEDRIVER 路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26191142/
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-08-22 22:29:10  来源:igfitidea点击:

Selenium Nodejs CHROMEDRIVER path

javascriptnode.jsselenium

提问by CodeGuru

Tried with "npm install selenium-webdriver" I'm still getting the error below. Any idea where the path is sops to be at?

尝试使用“npm install selenium-webdriver”我仍然收到以下错误。知道路径在哪里吗?

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.
    at Error (<anonymous>)
    at new ServiceBuilder (/var/www/nodejs/node_modules/selenium-webdriver/chrome.js:51:11)
    at getDefaultService (/var/www/nodejs/node_modules/selenium-webdriver/chrome.js:216:22)
    at new Driver (/var/www/nodejs/node_modules/selenium-webdriver/chrome.js:470:32)
    at Builder.build (/var/www/nodejs/node_modules/selenium-webdriver/builder.js:302:14)
    at Object.handle (/var/www/nodejs/node.js:31:4)
    at next_layer (/var/www/nodejs/node_modules/express/lib/router/route.js:103:13)
    at Route.dispatch (/var/www/nodejs/node_modules/express/lib/router/route.js:107:5)
    at c (/var/www/nodejs/node_modules/express/lib/router/index.js:195:24)
    at Function.proto.process_params (/var/www/nodejs/node_modules/express/lib/router/index.js:251:12)

回答by BlinkingCahill

Ok assuming you are using Windows please try the following steps:

好的,假设您使用的是 Windows,请尝试以下步骤:

  • Download the latest version of ChromeDriver from here ChromeDriver

  • Extract the zip and place the contents somewhere you know where it is for example "C:\Users\UserName\AppData\ChromeDriver"

  • Go to your Control Panel -> System -> Edit the System Variables. Click on the "environment variables" button.

  • In the system variables box there will be a variable named "Path" select it and click edit. Copy and paste the path to the containing directory of the chromedriver.exe you downloaded onto the end of the variable value and end it with a semi-colon.

  • Click ok and again to close environment variables and again to close system properties.

  • Close and reopen your terminal window.

  • Run the command again.

  • 从这里下载最新版本的 ChromeDriver ChromeDriver

  • 解压缩 zip 并将内容放在您知道的位置,例如“C:\Users\UserName\AppData\ChromeDriver”

  • 转到您的控制面板 -> 系统 -> 编辑系统变量。单击“环境变量”按钮。

  • 在系统变量框中将有一个名为“Path”的变量,选择它并单击编辑。将您下载的 chromedriver.exe 的包含目录的路径复制并粘贴到变量值的末尾,并以分号结束。

  • 再次单击确定关闭环境变量,再次单击关闭系统属性。

  • 关闭并重新打开终端窗口。

  • 再次运行命令。

I hope this helps - there is a good tutorial here

我希望这有助于-有一个很好的教程在这里

回答by rarunp04

Even after adding the driver path in the System variables it didnt worked.

即使在系统变量中添加驱动程序路径后,它也不起作用。

But by creating & setting own default chrome service, it worked

但是通过创建和设置自己的默认 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 Aditya

You only need to install npm install selenium-webdriver. Then download the chromedriver from here.

您只需要安装npm install selenium-webdriver. 然后从这里下载 chromedriver 。

const path = require('path');
const { ServiceBuilder } = require('selenium-webdriver/chrome');
const { Builder } = require('selenium-webdriver');

const geckoDriverPath = path.join(__dirname, "geckodriver"); // or wherever you've your geckodriver
const serviceBuilder = new ServiceBuilder(geckoDriverPath);
const SeleniumDriver = await new Builder()
  .forBrowser('chrome')
  .setFirefoxService(serviceBuilder)
  .build();

回答by Miguel Sánchez Villafán

I wanted to have chromedriver downloaded with my npm installcommand so I installed chromedriverfrom npm by

我想用我的npm install命令下载 chromedriver,所以我chromedriver从 npm安装

npm install --save chromedriver

npm install --save chromedriver

but then I was left with the question of how to set the path and I ended up in this question.

但是后来我遇到了如何设置路径的问题,我最终回答了这个问题。

If you use this method as well, according with the npm-chromedriver docsyou can do

如果你也使用这种方法,根据npm-chromedriver docs你可以做

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

It does work as expected for me, notice that chromedriver is not imported by assigning the requireresult to a variable but just as is

它确实按我的预期工作,请注意 chromedriver 不是通过将require结果分配给变量来导入的,而是按原样导入

回答by AWANISH

You can put the browser driver in the same location that you store your code for execution.

您可以将浏览器驱动程序放在存储执行代码的同一位置。

Hope it helps

希望能帮助到你

回答by alf

Under MacOSX the issue is likely to be caused by a wrong expansion of ~ to the hme directory. Set an absolute path instead of relying upon ~ and it works; at least on my computer.

在 MacOSX 下,这个问题很可能是由 ~ 错误扩展到 hme 目录引起的。设置绝对路径而不是依赖 ~ 并且它有效;至少在我的电脑上。