/dev tips

(123) The next version of Workspaces Optimise your web development workflow 2016 (125)

Selenium WebDriver: Learn to automate a browser with WebDriver and JavaScript

1st December 2016
Learn to automate a browser with WebDriver and JavaScript

I gave a talk on automating a browser in JavaScript (useful for scraping data, testing, etc.) at SeleniumConf 2016. You can:

Here's an example of counting the number of Wiki hyperlinks on a Wiki page.

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

const browser = new webdriver
    .Builder()
    .usingServer()
    .withCapabilities({
        'browserName': 'chrome'
    }).build();

async function start() {
    browser.get('http://en.wikipedia.org/wiki/Wiki');
    const links = await browser.findElements(webdriver.By.css('[href^="/wiki/"]'))
    console.log('Found', links.length, 'Wiki links.' )
    browser.quit();
}

start();

In Node v7, you can run it like this:

$ npm i selenium-webdriver
$ node --harmony-async-await Wiki.js
> Found 396 Wiki links.

Sign up to receive a developer tip, in the form of a gif, in your inbox each week.