0

I'm trying to get an automated Google search to click on the first link. So far I have not been successful and was wondering if someone could assist me. The search results populate, although the act of clicking the first link fails every time.

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

resultlink = driver.find_Element(:link, "Google Translate")
resultlink.click
2
  • find_Element -> find_element. Ruby is case-sensitive Commented Feb 27, 2014 at 5:06
  • 1
    Still no luck. Thanks for the input though. I am just learning how to do this kind of stuff. I have tried a few different ways of the .click script and nothing. Someone had told me something about there being some behind the scene stuff going on with Google, along the lines of 'Google Instance,' I'm not a 100% but could that be the problem? They said Google tries to block automation. Commented Feb 27, 2014 at 5:22

3 Answers 3

1

How about you try locating the First link using CSS selectors, something like this:

driver.find_element(:css, "#rso li:nth-child(1) div > h3 > a").click

where the 1 in the brackets (after nth-child) refers to the first search result.

Also I may be wrong, but try :link_text instead of :link, something like this :

resultlink = driver.find_element(:link_text, "Google Translate")
resultlink.click
Sign up to request clarification or add additional context in comments.

2 Comments

@Amey, I thought of link_text as well, but it's actually the same as link if you look at the list of finders: selenium.googlecode.com/git/docs/api/rb/Selenium/WebDriver/…
Well then searching by CSS is the answer. :)
1

If you watch this while it's happening, you might notice that it's failing before the results load. This is probably the single most annoying aspect of automation: timing. I tried adding sleep(5) before defining the element and it worked. However, sleeps are generally bad, so you should instead give selenium a little leeway to find the element before deciding it doesn't exist. You do this through implicit waits. For example:

driver.manage.timeouts.implicit_wait = 5 #time in seconds

This sets the maximum time that selenium will allow for an element to load. If it finds it sooner, it will continue right away. For this reason, it is far more efficient than sleep. More info is available in the documentation. Set this any time before you need to find your element. Once set, this will apply for the remainder of your test. It's a good idea in general to allow for slight delays and/or network hiccups.

Comments

0

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. ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.