0

I want to click on all :javascript links on page that I am loading in Firefox using Selenium Ruby.

What could be the correct method for doing this? I did for simple links like this:

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.open}

Though its not working properly due to some error

Selenium Test.rb:6: private method `open' called for #<Selenium::WebDriver::Element:0x4c155f0> (NoMethodError)
        from Selenium Test.rb:6:in `each'
        from Selenium Test.rb:6

Can :javascript links can be clicked using find_element method? The problem I am facing here is that if it clicks one link successfully and open it , it crashes while going for next. How to keep this continue till all links in page gets clicked.

3
  • Can you provide an example of what you mean by a ':javascript link'? Commented Apr 26, 2012 at 4:22
  • Do these links navigate to another page? Or do they run javascript? Commented Apr 26, 2012 at 4:41
  • it should be like this <a href='javascript:mc.s2c.id(7).key("ao1ZGq2pmJDE_j_Q").title("Get Engadget for Mobile").message("Engadget for Mobile").linkColor("#00AAFF").border("5px solid #000000").iconUrl("http://www.blogsmithmedia.com/www.engadget.com/media/appengadget.png").emailHint().linkUrl("http://ddr.aoltxt.com/deviceDetect?appId=7").emailHint().department("S2CW: www.engadget.com: Universal Widget").sendColorDark("#000000").sendColorLight("#444444").show()'>Universal</a> or between script tags s_265.linkInternalFilters="javascript:,engadget.com"; Commented Apr 26, 2012 at 6:52

2 Answers 2

1

I see two problems with the script:

  1. You are trying to use private method of Element class. To open link you need to all element.click, not element.open: This should work

    driver.find_elements(:tag_name, "a")[0].click

  2. Don't iterate through the links on the page an try to click them without assuring that you get back to initial page before the next click. Otherwise Selenium loses context and will give you following message:

    Selenium::WebDriver::Error::StaleElementReferenceError

Sign up to request clarification or add additional context in comments.

Comments

0

Try below code.I am not that much familiar to Ruby,Still below code may be helpful for you

link = driver.find_elements(:tag_name, "a")
i = 0
link.times do
{
link[i].click
driver.navigate.back
i=i+1
link = driver.find_elements(:tag_name, "a")
}

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.