Selenium Webdriver with Ruby - Basic Usage Example

Last updated:

This is a very basic example on how to use Selenium to navigate to and operate on a website at a very basic level. This script instructs selenium to:

  • navigate to;

    • the extra parameter at the end of the url is used to make sure the international google version is loaded
  • write "football" in the search input;

    • note that a \n (newline) is added to the end of the text added; this is done in order to simulate an enter key being pressed. A CSS selector is used to tell selenium what input text should be sent to.
  • verify that the Wikipedia article on Football gets listed in the search results.

    • Note that we actually search for the string "Wikipedia, the free encyclopedia". We instruct selenium to wait for 5 seconds until the page source contains said string. This is done in order to allow some time for the page to load. If it isn't found, selenium exits with an error.

The code:

#!/usr/bin/env ruby
# load the required gems
gem 'selenium-webdriver'
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :firefox ""
driver.find_element(:css,"form input[type='text']").send_keys("football\n")
wait = => 5)
wait.until { driver.page_source.include?("Wikipedia, the free encyclopedia") } 


Dialogue & Discussion