First, if you are learning Selenium, don't use any of the Google pages to start with. They look simple but are extremely tricky and complex under the hood. Find another website to automate please. It is against Google's user agreement anyway.
Then I can provide you working code. Note Google search results may render differently in different browsers, and you also need to use WebDriverWait for waiting.
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :firefox
driver.navigate.to "http://google.com"
element = driver.find_element(:name, 'q')
element.send_keys "translate"
element.submit
wait = Selenium::WebDriver::Wait.new(:timeout => 10)
wait.until {
driver.find_element(:css , 'h3 > a')
}
# click first result
# driver.find_element(:xpath , '(.//h3/a)[1]').click
results = driver.find_elements(:css , 'h3 > a')
results.each { |result|
if result.attribute('textContent') == 'Google Translate'
result.click
break
end
}
(.//h3/a)[1] means the first result. In Firefox, results don't have unique data-href for identifying, so you need to use index.
Otherwise, you can loop through all result links for a link that its attribute textContent equals Google Translate. Note the link text for it is actually Google <em>Translate</em>, so using text() in XPath might not work.
If you find the solution above is too much to take in, it proves you shouldn't start learning Selenium using Google pages in the first place. ;)
find_Element->find_element. Ruby is case-sensitive