Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing require('chromedriver).path directly to selenium-webdriver

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?

like image 770
jt000 Avatar asked Jan 01 '15 19:01

jt000


People also ask

How do I give permission to Chromedriver?

For Windows, Allow Read & Execute Permissions on chromedriver.exe for Everyone: Right Click chromedriver.exe > Properties On ChromeDriver.

Do we need to install Chromedriver for Selenium?

As Google Chrome dominates the browser market, the use of a ChromeDriver becomes a must. Selenium WebDriver uses the ChromeDriver to communicate test scripts with Google Chrome.

Does Chromedriver require Chrome?

ChromeDriver expects you to have Chrome installed in the default location for your platform.


2 Answers

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();
like image 190
jt000 Avatar answered Sep 21 '22 18:09

jt000


You can also do this:

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

const driver = new webdriver.Builder()
    .withCapabilities(webdriver.Capabilities.chrome())
    .build();
like image 21
mucsi96 Avatar answered Sep 19 '22 18:09

mucsi96