简体   繁体   中英

Selenium Nodejs CHROMEDRIVER path

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

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)

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

  • 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.

I hope this helps - there is a good tutorial here

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

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

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();

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

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();

I wanted to have chromedriver downloaded with my npm install command so I installed chromedriver from npm by

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 docs you can do

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 require result to a variable but just as is

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

Hope it helps

In my case, my terminal was already opened before I set PATH. All solutions failed until I finally restarted the terminal.

install chromedriver

npm install --save-dev chromedriver

const chrome = require('selenium-webdriver/chrome');
const {Builder, By, Key, until,Capabilities} = require('selenium-webdriver');
const geckoDriverPath  = require('chromedriver').path;

let service = new chrome.ServiceBuilder(geckoDriverPath ).build();
chrome.setDefaultService(service);

(async function example() {
    let driver = await new Builder().withCapabilities(Capabilities.chrome()).build();
    try {
        await driver.get('https://www.baidu.com/');
        await driver.findElement(By.name('wd')).sendKeys('webdriver', Key.RETURN);
        await driver.wait(until.titleIs('webdriver_百度搜索'), 1000);
        console.log('ok')
     } finally {
        await driver.quit();
     }
  })();

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM