Skip to content

Instantly share code, notes, and snippets.

@jagdeepsingh
Created May 21, 2020 11:57
Show Gist options
  • Save jagdeepsingh/5028392766b272c7a0c3722d376d5ebb to your computer and use it in GitHub Desktop.
Save jagdeepsingh/5028392766b272c7a0c3722d376d5ebb to your computer and use it in GitHub Desktop.
Selenium Webdriver with Ruby

Selenium Webdriver

1. Config

  • Ruby v2.5.1
  • macOS Catalina 10.15

2. Install gems

Open terminal and run gem install selenium-webdriver.

$ gem list | grep selenium-webdriver
selenium-webdriver (3.142.7, 3.142.3)

Run gem install webdrivers in terminal.

$ gem list | grep webdrivers
webdrivers (4.3.0, 4.0.0)

3 Usage

Require the gems:

require 'selenium-webdriver'
require 'webdrivers'

Initialize a session:

driver = Selenium::WebDriver.for :chrome # OR :safari, :firefox, etc.
driver.get WEBPAGE_URL

3.1 Wait

... for content to appear on the page.

wait = Selenium::WebDriver::Wait.new(timeout: 10) # in seconds
element = wait.until { driver.find_element(id: 'myform') }

To find all elements matching a criteria, you can do driver.find_elements(...).

3.2 Find by Xpath

Search for a submit button anywhere in the page:

element = driver.find_element(xpath: "//button[@type='submit']")

You can click the submit button with element.click.

3.3 Textfield input

Enter a value:

element.send_keys('jagdeepsingh.125k@gmail.com')

Clear existing value:

element.clear

3.4 Quit driver

driver.quit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment