Skip to content

Instantly share code, notes, and snippets.

@NanoDano
Created July 31, 2016 18:25
Show Gist options
  • Save NanoDano/e08718abd1ca5f9e465d57f4684c67d8 to your computer and use it in GitHub Desktop.
Save NanoDano/e08718abd1ca5f9e465d57f4684c67d8 to your computer and use it in GitHub Desktop.
Ruby Selenium Example
require 'selenium-webdriver'
browser = Selenium::WebDriver.for :firefox
# Load a page
begin
browser.navigate.to 'http://www.devdungeon.com'
rescue
puts 'Error loading page.'
end
# Get single element by ID
element = browser.find_element(:id, 'node-155')
puts element.text
# Get multiple elements by class
browser.find_elements(:class_name, 'title').each do |e|
puts e.text
end
# Get element using XPath
element = browser.find_elements(:xpath, "html/body/div[1]")
puts element
# Enter text in search box
element = browser.find_element(:id, 'edit-search-block-form--2')
element.send_keys 'ruby'
# Click the search button
element = browser.find_element(:id, 'edit-submit')
element.click
browser.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment