0

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();
Bennett Barouch
  • 125
  • 1
  • 3
  • 10

2 Answers2

0

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

Satish Gupta
  • 1,447
  • 1
  • 12
  • 14
  • Given that this is a copy and paste of the example on the Selenium site, I don't know what it means that it can't find webdriver. There is no such thing as an include or import in javascript, and there is no indication that any previous script is supposed to have been prepended to the code I've shown. Thus, my not knowing what to do to resolve it. – Bennett Barouch Aug 22 '16 at 01:21
  • When I follow other online examples and begin with selenium = require("selenium-webdriver"); I get a different error message about not finding the chrome binary, even though it is on PATH. – Bennett Barouch Aug 22 '16 at 01:31
  • I updated the answer to include the working sample for javascript. you were right that we need require("selenium-webdriver") which is equivalent to import to tell script about the webdriver module. I was able to execute script, it launched google and searched for cheese. Reference : http://seleniumhq.github.io/selenium/docs/api/javascript/index.html – Satish Gupta Aug 22 '16 at 07:25
  • Thank you, @Satish. I realized it was chrome and not chromeDriver that could not be found. I added var co = new ChromeOptions(); co.setBinary('/custom/path'); var driver = new webdriver.Builder().setChromeOptions(co) ... and then I got "ReferenceError: ChromeOptions is not defined" – Bennett Barouch Aug 22 '16 at 14:22
-1

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?

Bennett Barouch
  • 125
  • 1
  • 3
  • 10
  • 1
    Likely should have been an edit to the post and/or possibly some additional questions. Also "hi" from T.A.'s son-in-law. – Joshua Drake Aug 10 '17 at 21:13