20

tl;dr: Does anyone know how to pass the path of chromedriver to selenium-webdriver in code without setting the PATH environment variable?

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

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.

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"
},

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?

jt000
  • 3,196
  • 1
  • 18
  • 36

3 Answers3

48

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();
jt000
  • 3,196
  • 1
  • 18
  • 36
  • I am new to node.js, is there a mistake in second line as there is no chrome inside selenium-webdriver then too you are writing ` var chrome=require('selenium-webdriver/chrome');` . After doing all this, still i am getting an error "SyntaxError: Error parsing /home/abc/node_modules/selenium-webdriver/package.json: Unexpected token / " – Abhishek Tripathi Jan 24 '15 at 20:53
  • 1
    require('selenium-webdriver/chrome') means to load the chrome.js file inside the selenium-webdriver npm installation. This should work just fine. However, I think you might have a corrupt package.json file (you shouldn't be seeing a parsing error from an unresolved reference). Did you make any changes to this file? – jt000 Jan 25 '15 at 22:26
  • yes the changes which you suggested to make in the package.json – Abhishek Tripathi Jan 25 '15 at 22:33
  • 1
    That should go in your package's package.json file. Not in the "selenium-webdriver/package.json" as mentioned in the SyntaxError. – jt000 Jan 26 '15 at 12:59
  • I get this error Server terminated early with status 0 – albanx Nov 08 '18 at 17:21
8

You can also do this:

require('chromedriver');
const webdriver = require('selenium-webdriver');

const driver = new webdriver.Builder()
    .withCapabilities(webdriver.Capabilities.chrome())
    .build();
mucsi96
  • 314
  • 2
  • 4
  • 2
    This is as straightforward as it gets, I like it! However, instead of `';'`, you should use `path.delimiter`. – Stefan Dragnev Oct 22 '16 at 09:30
  • Does that solve the original question? It looks like you just inlined the "webdriver" variable from my answer above. Maybe I'm misunderstanding... – jt000 Sep 19 '17 at 14:32
  • 1
    The idea here is that you do not need to build a service. The trick here is that `require('chromedriver');` will add chromedriver path to the `process.env.PATH`. Because of that `selenium-webdriver` will be able to start the chromedriver if driver with `crome` capability is requested. – mucsi96 Sep 23 '17 at 07:57
0
Option 1:
process.env.PATH = 'path to chrome driver binary folder';
var driver = new Builder().forBrowser('chrome').build();

Option 2:
install chromedriver (npm install chromedriver)
require('chromedriver'); in your code

//chrome driver will automatically look for chromedriver or chromedriver.exe in the path mentioned based on OS
reference: 
https://github.com/SeleniumHQ/selenium/blob/master/javascript/node/selenium-webdriver/chrome.js line 142
https://github.com/SeleniumHQ/selenium/blob/master/javascript/node/selenium-webdriver/io/index.js line 237
irlatech
  • 67
  • 6
  • Thanks, Option 1 might work, but Option 2 uses the PATH environment variable which the question specifically stated that it didn't want to set. – jt000 Mar 09 '20 at 13:41