user6214
user6214

Reputation: 69

Finding element and entering in Ruby Selenium

I'm new to the Selenium and Ruby and Cucumber/Gherkins world and am trying a simple script to navigate to the Google page, find the search bar and enter a word and press enter or find the "Google Search" element and click.

This is in a Ruby file but formatted in Gherkins as I'm working with it.

require 'rubygems'
require 'selenium-webdriver'

driver = Selenium::WebDriver.for :chrome

Given(/^I am on the Google website$/) do
  driver.navigate.to "http://www.google.com"
end

When(/^search is entered$/) do
  search = driver.find_element(xpath: "//div[@class = 'jhp big']//input[@class = 'gLFyf gsfi']")
  search.send_keys "this"
end

Then(/^confirm$/) do
  puts "Confirmed"
  driver.close
end

So here I'm navigating to the google website using a Selenium WebDriver initialized as driver. Then finding the element using the xpath and sending in the word 'this'.

When I run this, I get this error:

Selenium::WebDriver::Error::NoSuchElementError: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class = 'jhp big']//input[@class = 'gLFyf gsfi']"}

Also, to click 'enter' I can either do send_keys :enter or find the Search button and use 'click' correct?

Thank you in advance

Upvotes: 1

Views: 2046

Answers (1)

Rajagopalan
Rajagopalan

Reputation: 6064

you have to use implicit wait for driver to wait until element found Write the following code and then fit into your cucumber model

require 'selenium-webdriver'
driver=Selenium::WebDriver.for :chrome
driver.navigate.to("https://www.google.com/")
driver.manage.timeouts.implicit_wait=10
driver.find_element(name: 'q').send_keys 'raja'
driver.find_element(name: 'btnK').click

But remember, If you WATIR which is a wrapper around selenium binding these kind of waiting is automatic.

Upvotes: 2

Related Questions