Selenium 2 is used primarily for testing web applications by automating the web browser’s actions.

For instance, forms must be filled in and submitted constantly. Selenium 2 can automate the browser to do it instead.

Installation

Install selenium-webdriver using npm:

$ npm install selenium-webdriver

You must download (link above) a driver for the browser and add it to the PATH. This tutorial uses Firefox which should not require a driver unless you have the latest version.

The latest version of Firefox (47), as of this writing, requires marionette:

Download and extract the geckodriver binary; rename it to wires and add it to your PATH.

Examples

Open a webpage:

var webdriver = require('selenium-webdriver');
// these 3 lines are required with firefox 47+ 
var Capabilities = require('selenium-webdriver/lib/capabilities').Capabilities;
var capabilities = Capabilities.firefox();
capabilities.set('marionette', true);

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

driver.get('https://youtube.com');

Input text into a search form and click submit:

var webdriver = require('selenium-webdriver');
// these 3 lines are required with firefox 47+ 
var Capabilities = require('selenium-webdriver/lib/capabilities').Capabilities;
var capabilities = Capabilities.firefox();
capabilities.set('marionette', true);

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

driver.get('https://youtube.com');

driver.findElement(webdriver.By.id('masthead-search-term')).sendKeys('cats');
driver.findElement(webdriver.By.id('search-btn')).click();

Selenium 2 Youtube Search