Selenium WebDriver Basics – Ruby


1. Install Ruby

2.  In Command Prompt >gem install selenium-webdriver

3. Copy and Paste the below code and save the file as sample_raveendran.rb

require ‘rubygems’
require “selenium-webdriver”

driver = Selenium::WebDriver.for :ie “”

if driver.title == “Google”
puts “Testcase 1 passed”
puts “Testcase 1 failed”

q = driver.find_element(:name, ‘q’)
q.send_keys “Raveendran Selenium”

sleep 5

if driver.title == “RAVEENDRAN SELENIUM – Google Search”
puts “Testcase 2 passed”
puts “Testcase 2 failed”


if q.include?(“”)
puts “Testcase 3 passed”
puts “Testcase 3 failed”
end “”
a=driver.find_element(:link,’Advanced search’)

if driver.title == “Google Advanced Search”
puts “Testcase 4 passed”
puts “Testcase 4 failed”

a.send_keys(“Raveendran Selenium”)


options.each do |g|
if g.text == “20 results”

a=driver.find_elements(:xpath=> ‘/html/body/table[2]/tbody/tr/td/table/tbody/tr/td/div


a.each do |b|

sleep 4

if driver.title == “RAVEENDRAN SELENIUM – Google Search”
puts “Testcase 5 passed”
puts “Testcase 5 failed”

4. Run the ruby code

5. The Output looks like

Testcase 1 passed
Testcase 2 passed
Testcase 3 passed
Testcase 4 passed
Testcase 5 passed


11 thoughts on “Selenium WebDriver Basics – Ruby

  1. Hi Raveendran,

    Thanks for this post. Do you have any suggestion on how to check for an element based on more than 1 attributes eg based on id and class?

    a=driver.find_element(:link,’Advanced search’ || :id, ‘advSearch’)


    1. Hi Anil,

      My Question is –> Why you want to mention 2 attributes to identify the single element ? You can use anyone of the unique attribute to automate the field.

  2. Thanks Raveendran. Actually There are variety of attributes that are either common or unique but there no one attribute that is unique … I used xpath for now … It seems to work. Thanks

  3. HI Raveendran, Iam new to selenium, and I want to learn more about webdriver with ruby, Iam not a Automation tester but interested so is there a way you can guide me.

  4. Hi
    I want to learn selenium-web driver with ruby language , can u pls suggest me how could find resource for this

  5. Hello;
    Running your code and DOS window opens/closes very fast. IE browser never opens.
    Any idea why? Other RUBY/Selenium code runs fine, so I don’t think it’s the environment. Thanks

  6. I am using Selenium webdriver with TestNG and i have created a small framework using this. But I am not aware that whether it would help me in working witj selenium in any company or not bcuause I do not know much about it.
    I am stuck in site where I am unable to identify an object. I have created an add in quikr and then whie editing that add, there is a customized listbox which I am not able to identify.
    Customized list box means:-it has main categry like Mobile/OS etc and after selecting the Category, it displays subcategories for that in the same customized list box. I am able to select the category but not able to Identify subcategory list box.
    Kindly help me here if you can.

    Also please let me know if you can provide me support on automating a webapplication and that site would be any site like quikr or any other site.
    I want your help because I want a job in Selenium very urgently and tehre are lot of things which I have not used I have not worked in selenium framework.
    I will pay you for this. Just help me please….


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s