Skip to content

neudabei/watir_learning_hour

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Watir WebDriver FutureLearn Learning Hour

Setup instructions

Introduction

Watir WebDriver can be used to give instructions to your browser in Ruby.

Watir WebDriver is popular for testing. In this learning hour we focus on using Watir for interacting with websites to browse through them and read data. Watir WebDriver uses Selenium-WebDriver under the hood.

The Watir syntax is very idiomatic. Once you create an instance of a Watir browser by calling Watir::Browser.new you can send it commands, such as browser.goto 'http://www.google.com'. If not run headless, which is the default, you can see a browser window and follow the executed instructions.

You can combine manual browsing with commands in Ruby. Verify how the current page, even after manual browsing is tracked by Watir. You can verify this by manually browsing to a different page from your Watir Browser window and calling browser.url.

The hardest part is often finding how to uniquely identify and access elements of interest on a site. Elements can be found by their HTML attributes, such as class, id, name, e.g. browser.button(:name => 'submit'), or by using xPath. Often there is more than one way to find information. Note that you can also ask Watir to return all types of an element on a page, e.g. browser.h1s will return all h1 headers on the present page. You can iterate over the returned results with Ruby's built in methods, such as Array#each or String#include?.

Get started with Watir

  • include the library: require 'watir-webdriver'
  • create an instance of a new browser: browser = Watir::Browser.new
  • direct the browser to a url: browser.goto 'http://www.futurelearn.com'
  • Fill out a simple form:
  require 'watir-webdriver'
  browser = Watir::Browser.new
  browser.goto 'bit.ly/watir-webdriver-demo'
  browser.text_field(:id => 'entry_1000000').set 'your name'
  browser.select_list(:id => 'entry_1000001').select 'Ruby'
  browser.select_list(:id => 'entry_1000001').selected? 'Ruby'
  browser.button(:name => 'submit').click
  browser.text.include? 'Thank you'

  # An example from https://watirwebdriver.com/
  • Get the names of Coursera's partners and write them to a file:
require 'watir-webdriver'
browser = Watir::Browser.new
browser.goto 'https://www.coursera.org/about/partners'

# Coursera lists they have 145 partners. 
browser.divs(class: 'partner-box-wrapper card-one-clicker flex-1').count
# => 145 indeed

# Get all partners names
scraped_list = browser.divs(class: 'partner-box-wrapper card-one-clicker flex-1').map(&:text)

class FileMaker
  require 'csv'
  attr_reader :scraped_list

  def initialize(scraped_list)
    @scraped_list = scraped_list
  end

  def save_file
    File.open("scraped_list.txt", "w+") do |file|
      file.puts scraped_list
    end
  end
end

FileMaker.new(scraped_list).save_file

Further reading

About

Learn about Watir WebDriver - https://watirwebdriver.com

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages