ruby selenium-webdriver watir-webdriver

There are many other Selenium gems out there, but this is the only official, maintained gem. If you’re looking for a slightly higher level API built on the same technology, you may want to check out watir-webdriver or capybara.

example code

require "selenium-webdriver"

driver = Selenium::WebDriver.for :firefox ""

element = driver.find_element(:name, 'q')
element.send_keys "Hello WebDriver!"

puts driver.title


# execute arbitrary javascript
puts driver.execute_script("return window.location.pathname")

# pass elements between Ruby and JavaScript
element = driver.execute_script("return document.body")
driver.execute_script("return arguments[0].tagName", element) #=> "BODY"

# wait for a specific element to show up
wait = => 10) # seconds
wait.until { driver.find_element(:id => "foo") }

# switch to a frame
driver.switch_to.frame "some-frame" # name or id
driver.switch_to.frame driver.find_element(:id, 'some-frame') # frame element

# switch back to the main document

# repositionning and resizing browser window:
driver.manage.window.move_to(300, 400)
driver.manage.window.resize_to(500, 800)

# get an attribute
class_name = element.attribute("class")

# is the element visible on the page?

# click the element

# get the element location

# scroll the element into view, then return its location

# get the width and height of an element

# press space on an element - see Selenium::WebDriver::Keys for possible values
element.send_keys :space

# get the text of an element

SSL Certificates

The Firefox driver ignores invalid SSL certificates by default. If this is not the behaviour you want, you can do:

profile =
profile.secure_ssl = true

driver = Selenium::WebDriver.for :firefox, :profile => profile

There is an edge case where the default SSL certificate check will not work correctly. WebDriver assumes that the certificate is untrusted whenever there’s a problem, which means a certificate from a trusted issuer but with a hostname mismatch (e.g. a production certificate in a test environment) will not be correctly ovverriden. See UntrustedSSLCertificates for more on why this is. To work around it, tell the Firefox driver to not assume the issuer is untrusted:

profile =
profile.assume_untrusted_certificate_issuer = false
driver = Selenium::WebDriver.for :firefox, :profile => profile

Not that Profile#secure_ssl remains set to the default value of true in the above example.