Akash Panchal
Akash Panchal

Reputation: 305

How to click on all links from fetched using Selenium Ruby

I have written following lines to click all links in fetched page but it clicks on one link only and stuck there clicking , I have used Selenium Web Driver API:

require 'rubygems'
require 'selenium-webdriver'

driver = Selenium::WebDriver.for :firefox
driver.get " http://www.testfire.net "
for i in 1..100
    link = driver.find_element(:tag_name, "a")
    link.click
end

tell me how can I skip clicked link and go to next one or can set the range from 1 to until it reaches end of html page at </html> tag. I think it would be like eofpage = drive.find_element(:tag_name, "/html")

Upvotes: 3

Views: 2376

Answers (2)

Thiyagarajan Veluchamy
Thiyagarajan Veluchamy

Reputation: 515

we can use bellow code to find the all links in a page and open with new tab.

require 'rubygems'
require 'selenium-webdriver'

driver = Selenium::WebDriver.for :chrome
@driver.get "http://thiyagarajan.wordpress.com/"
  link = @driver.find_elements(:tag_name, "a")
  link.each do |a|
    a = @driver.execute_script("var d=document,a=d.createElement('a');a.target='_blank';a.href=arguments[0];a.innerHTML='.';d.body.appendChild(a);return a", a)
    a.click
  end

Upvotes: 1

Brian Underwood
Brian Underwood

Reputation: 10856

It looks like there is a driver.find_elements method:

require 'rubygems'
require 'selenium-webdriver'

driver = Selenium::WebDriver.for :firefox
driver.get " http://www.testfire.net "

driver.find_elements(:tag_name, "a").each {|link| link.click }

Upvotes: 4

Related Questions