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.

4

2 回答 2

1

我看到脚本有两个问题:

  1. 您正在尝试使用 Element 类的私有方法。要打开链接,您需要所有 element.click,而不是 element.open:这应该可以

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

  2. 不要遍历页面上的链接并尝试单击它们,而不能确保在下次单击之前返回初始页面。否则 Selenium 会失去上下文,并会给你以下消息:

    Selenium::WebDriver::Error::StaleElementReferenceError

于 2013-05-13T07:58:03.707 回答
0

试试下面的代码。我对 Ruby 不太熟悉,下面的代码可能对你有帮助

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")
}
于 2013-05-13T09:57:20.483 回答