33

I’m using node 5.10.0 on Linux. Having some issues running my script, which are displayed below

[davea@mydevbox mydir]$ node SkyNet.js 
Validation Complete
/home/davea/node_modules/selenium-webdriver/chrome.js:185
      throw 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.
    at Error (native)
    at new ServiceBuilder (/home/davea/node_modules/selenium-webdriver/chrome.js:185:13)
    at getDefaultService (/home/davea/node_modules/selenium-webdriver/chrome.js:362:22)
    at Driver (/home/davea/node_modules/selenium-webdriver/chrome.js:771:34)
    at Builder.build (/home/davea/node_modules/selenium-webdriver/builder.js:464:16)
    at Object.<anonymous> (/home/davea/mydir/js/Optimus.js:14:4)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)

It is saying chromedriver isn’t on my path, but I just downloaded the appropriate version from here — http://chromedriver.storage.googleapis.com/index.html?path=2.9/ , and as you can see, it is on my PATH

[davea@mydevbox mydir]$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/davea/bin:/home/davea/bin:/usr/lib/chromedriver

with the following permissions …

[davea@mydevbox mydir]$ ls -al /usr/lib/chromedriver
-rwxr-xr-x 1 davea evotext 5503600 Feb  3  2014 /usr/lib/chromedriver

So I am confused as to why I’m getting this error. Any help is appreciated, - Dave

Dave
  • 15,639
  • 133
  • 442
  • 830

8 Answers8

47

To add to Niels' answer, for those not using Babel

  1. First install the chromedrive package using npm. If you install globally ensure to have node packages in your path
npm install -g chromedriver

If PATH errors persist, just save it to the local project's dependencies

npm install --save-dev chromedriver
  1. For those not using Babel
const webdriver = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
const chromedriver = require('chromedriver');

chrome.setDefaultService(new chrome.ServiceBuilder(chromedriver.path).build());
Nick Mitchell
  • 1,207
  • 13
  • 24
  • 1
    This works and thanks. Those should be `const` and not `let`. Also, you might want to include the instantiation of the usable driver after you've made the setting happen. – Neil Gaetano Lindberg Jan 31 '20 at 15:30
  • Yep, downloading the driver and providing the $PATH is not sufficient. I had to use npm install -g chromedriver – Yasin Bekar Sep 13 '21 at 18:17
17

I had the same issue. I have resolved it by taking the path from chromedriver package.

Here is my code:

import webdriver from 'selenium-webdriver';
import chrome from 'selenium-webdriver/chrome';
import chromedriver from 'chromedriver';

chrome.setDefaultService(new chrome.ServiceBuilder(chromedriver.path).build());

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

Which is based on the code from this answer: Passing require('chromedriver).path directly to selenium-webdriver

Community
  • 1
  • 1
Niels van Reijmersdal
  • 2,038
  • 1
  • 20
  • 36
  • This is great, but it requires knowledge of babel and transpiling. I just want to point out that all the `import varariableName from 'package-name'` could be: `const variableName = require('package-name');` in the simplest form. – Neil Gaetano Lindberg Jan 31 '20 at 15:32
10

Might be a little late but if someone comes across this problem for me worked the following:

First npm install -g chromedriver --save

Then add this line on top of your code require('chromedriver');

Here is a demo:

require('chromedriver');
var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder()
  .forBrowser('chrome')
  .build();
driver.get('https://google.com');

For more details you can go here: https://www.npmjs.com/package/chromedriver

x4bibi
  • 109
  • 1
  • 6
  • 1
    What do you mean add this at the top of our code? I don't have any code, I have a .side file. Where does this code go? – pabrams Jul 09 '19 at 21:14
  • It's using nodejs so it'll need to be a js file. Add to the top of your e2e test e.g. `VisitGoogle.js`. `driver.get('https://google.com');` is an example of telling chrome driver to visit google.com. For more details you'll need to read the docs – Nick Mitchell May 06 '20 at 00:19
  • You are saving chromedrive globally and to the package dependencies. This will both save chromedrive globally and also to `node_modules`. npm resolution will look in `node_modules` first, hence global install is not needed. This is double handling and confusing. You should either (1) save globally `-g` and add to `peerDependencies`; or (2) save to dependencies `-s` or `--save` – Nick Mitchell Apr 04 '22 at 00:46
3

Just Download the driver .exe file into the same folder as your current project file is lying. It worked for me. Might work for you as well. Peace!

COOL ED
  • 31
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 17 '22 at 10:40
2

for my small selenium-webdriver test i did these steps after i researched online and here:

  1. npm install selenium-webdriver
  2. npm install chromedriver
  3. npm install geckodriver and opened file library.js and npm init and ran node library.js (source code below)
  4. Error: (node:14212) UnhandledPromiseRejectionWarning: NoSuchSessionError: invalid session id Some long error related to not same chromedriver version. so i checked the chrome browser version manually in the browser. it was version 73 and my mistake i had downloaded chromedriver version 74.0.
  5. so go to https://chromedriver.storage.googleapis.com/index.html?path=73.0.3683.68/ download according to your OS and download it in ~/Downloads .
  6. then in open terminal in the ~/Downloads folder.
  7. then USER@DESKTOP:~/Downloads$ unzip ~/Downloads/chromedriver_linux64.zip -d ~/Downloads You will get the raw chromedriver file in ~/Downloads folder.
  8. now i moved the ChromeDriver 73.0.3683.68 file to two places - usr/local/bin and usr/bin in my system.
  9. there was already a chromedriver file in usr/local/bin .
  10. to move the file - USER@DESKTOP:~/Downloads$ sudo mv -f ~/Downloads/chromedriver /usr/local/bin/chromedriver and USER@DESKTOP:~/Downloads$ sudo mv -f ~/Downloads/chromedriver /usr/bin/chromedriver you are saying you want to move the file chromedriver from first location to other means replacing any files already in those locations with same name.

  11. Last all i did was. close the vscode and relaunched it. and ran my code node library.js . and it worked it the chrome browser for me.

SOURCE CODE : LIBRARY.JS

var webdriver = require('selenium-webdriver');

var By = webdriver.By;

var until = webdriver.until;

var driver = new webdriver.Builder().forBrowser('chrome').build();

driver.get('https://www.google.com');
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
suryavansh
  • 191
  • 2
  • 10
0

The easiest way for me is to use a local install of chromedriver. I got the error when I forgot to require chromedriver inside my script.

  1. run npm install
  2. in your node script have the line require('chromedriver'); before you call a webdriver (I am using it with selenium-webdriver)

Not sure why it's not detecting your global path. If I don't have the line require('chromedriver') I can add the local chromedriver file to the path and it works.

Dmitri R117
  • 2,502
  • 23
  • 20
0

been into a lot of confusion due to this for Jenkins windows slave

So here is how you can fix it

Following this link and download compatible driver as per your chrome version -

https://www.selenium.dev/documentation/webdriver/getting_started/install_drivers/

Once driver is downloaded place it somewhere in c:\ drive like c:\chromedriver

Add that path to your System environment variable - Reference is here https://www.browserstack.com/guide/run-selenium-tests-using-selenium-chromedriver

Once that is done please note to ensure to reboot your windows machine else Jenkins won't be able to find it

One more debug tip - I initially installed it using npm globally and added C:\Users\some_windows_user\AppData\Roaming\npm\node_modules to system environment variable and rebooted my machine. That never worked in CI.

So like I said above move it c:\chromedriver and add to System environment variable and 100% reboot you machine

TARJU
  • 1,785
  • 1
  • 14
  • 16
0

Just puth this -

require("chromedriver");

before this -

let driver = new webdriver.Builder().forBrowser('chrome').build();