Skip to content

Instantly share code, notes, and snippets.

@Tanapruk
Last active March 20, 2017 07:36
Show Gist options
  • Save Tanapruk/2566f1aaf245a41e75ab79739a44e207 to your computer and use it in GitHub Desktop.
Save Tanapruk/2566f1aaf245a41e75ab79739a44e207 to your computer and use it in GitHub Desktop.
Selenium Web Driver

Run A Script

To run a simple script, for example, google_search.js

const {Builder, By, until} = require('selenium-webdriver');
new Builder()
    .forBrowser('firefox')
    .build()
    .then(driver => {
      return driver.get('http://www.google.com/ncr')
        .then(_ => driver.findElement(By.name('q')).sendKeys('webdriver'))
        .then(_ => driver.findElement(By.name('btnG')).click())
        .then(_ => driver.wait(until.titleIs('webdriver - Google Search'), 1000))
        .then(_ => driver.quit());
    });

1. Create a folder and a file

mkdir testselenium
vim google_search.js 
# then paste above codes

2. Install dependencies

npm install selenium-webdriver #it has to be locally installed.
npm install -g geckodriver #when using firefox, the script required geckodriver to be environmental variables.

3. Run script

node google_search.js

Alternative

If you have selenium-standalone running, you can send your .js file to the url as follows:

SELENIUM_REMOTE_URL=http://0.0.0.0:4444/wd/hub node google_search.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment