«   Previous tip Next tip   »

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

Last updated: 19th July 2020
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.
«   Previous tip Next tip   »

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