'Selenium Webdriver Node.js

I am new to Selenium, and new to Node.js. I've done the npm installs and put chromedriver and geckodriver in a directory on my PATH. I am on Mac OS X. Running 'node cheese.js', I immediately get:

cheese.js:1
(function (exports, require, module, __filename, __dirname) { var driver = new webdriver.Builder().build();

ReferenceError: webdriver is not defined
    at Object.<anonymous> (/Users/bjbarouch/Sites/cheese.js:1:80)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)
    at node.js:974:3

For reference, the cheese code is:

var driver = new webdriver.Builder().build();
driver.get('http://www.google.com');

var element = driver.findElement(webdriver.By.name('q'));
element.sendKeys('Cheese!');
element.submit();

driver.getTitle().then(function(title) {
    console.log('Page title is: ' + title);
});

driver.wait(function() {
    return driver.getTitle().then(function(title) {
        return title.toLowerCase().lastIndexOf('cheese!', 0) === 0;
    });
}, 3000);

driver.getTitle().then(function(title) {
    console.log('Page title is: ' + title);
});

driver.quit();


Solution 1:[1]

This error is clearly not because of chromedriver or geckodriver path issue. It is complaining about not being able to resolve webdriver. I am also not very familiar with javascript code. But pretty sure you need to import webdriver module to tell cheese.js about the webdriver.

EDIT : Below is the working script. Steps : Place the chromedriver binary in path 1. Install Node.js to be able to run javascript, it will also install npm 2. Copy the below script in some file in a dir 3. cd this dir 4. npm install selenium-webdriver 5. node scirpt.js

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

driver.get('http://www.google.com');

var element = driver.findElement(webdriver.By.name('q'));
element.sendKeys('Cheese!');
element.submit();

driver.getTitle().then(function(title) {
  console.log('Page title is: ' + title);
});

driver.wait(function() {
  return driver.getTitle().then(function(title) {
    return title.toLowerCase().lastIndexOf('cheese!', 0) === 0;
  });
}, 3000);

driver.getTitle().then(function(title) {
  console.log('Page title is: ' + title);
});

driver.quit();

Before running this script, you need to have selenium-webdriver package installed. for this run :npm install selenium-webdriver

Solution 2:[2]

Okay -- here is what is requried in the Node.js context if you have Google Crhome installed in a non-standard location:

var webdriver = require('selenium-webdriver');
var chrome = require('selenium-webdriver/chrome');
var co = new chrome.Options();
co.setChromeBinaryPath("/Applications/UsrBin/Google\ Chrome.app/Contents/MacOS/Google\ Chrome");
var driver = new webdriver.Builder()
    .forBrowser("firefox")
    .setChromeOptions(co)
    .build();

console.log("hello");
driver.quit();

Why does it work if I say forBrowser("firefox") but crash if I don't?

What is the URL for the complete guide on build() and setting browser options?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Bennett Barouch