1

我想从我使用加载的页面打开新浏览器选项卡中的所有点击链接driver.get "http://www.example.com"。我在之前问过的其他问题中得到了答案,但它没有按我预期的那样工作。代码如下:

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 }

运行此代码后,Selenium Web Driver 会打开 Firefox,加载请求的页面并单击它找到的第一个锚标记,然后它会因错误而崩溃。

我也试过这个driver.find_element(:tag_name, "a").send_keys [:control,'t']这个打开新标签但没有加载页面,空白标签。

我怎样才能在循环中安排这个,这样我就不会出错,并且第一个加载页面上的所有点击链接都将在单独的浏览器选项卡中打开?

我需要使用wait它来工作吗?

[remote server] resource://fxdriver/modules/web_element_cache.js:9500:in `unknown': Element not found in the cache - perhaps the page has changed since it was looked up (Selenium::WebDriver::Error::StaleElementReferenceError)
        from [remote server] file:///C:/Users/AKASH/AppData/Local/Temp/webdriver-profile20120427-1496-sev0j4/extensions/fxdriver@googlecode.com/components/command_processor.js:9069:in `unknown'
        from [remote server] file:///C:/Users/AKASH/AppData/Local/Temp/webdriver-profile20120427-1496-sev0j4/extensions/fxdriver@googlecode.com/components/command_processor.js:9504:in `unknown'
        from [remote server] file:///C:/Users/AKASH/AppData/Local/Temp/webdriver-profile20120427-1496-sev0j4/extensions/fxdriver@googlecode.com/components/command_processor.js:10510:in `unknown'
        from [remote server] file:///C:/Users/AKASH/AppData/Local/Temp/webdriver-profile20120427-1496-sev0j4/extensions/fxdriver@googlecode.com/components/command_processor.js:10529:in `unknown'
        from [remote server] file:///C:/Users/AKASH/AppData/Local/Temp/webdriver-profile20120427-1496-sev0j4/extensions/fxdriver@googlecode.com/components/command_processor.js:10534:in `unknown'
        from [remote server] file:///C:/Users/AKASH/AppData/Local/Temp/webdriver-profile20120427-1496-sev0j4/extensions/fxdriver@googlecode.com/components/command_processor.js:10482:in `unknown'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.20.0/lib/selenium/webdriver/remote/response.rb:52:in `assert_ok'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.20.0/lib/selenium/webdriver/remote/response.rb:15:in `initialize'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.20.0/lib/selenium/webdriver/remote/http/common.rb:59:in `new'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.20.0/lib/selenium/webdriver/remote/http/common.rb:59:in `create_response'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.20.0/lib/selenium/webdriver/remote/http/default.rb:64:in `request'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.20.0/lib/selenium/webdriver/remote/http/common.rb:40:in `call'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.20.0/lib/selenium/webdriver/remote/bridge.rb:594:in `raw_execute'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.20.0/lib/selenium/webdriver/remote/bridge.rb:572:in `execute'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.20.0/lib/selenium/webdriver/remote/bridge.rb:354:in `clickElement'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.20.0/lib/selenium/webdriver/common/element.rb:34:in `click'
        from selclick.rb:6:in `block in <main>'
        from selclick.rb:5:in `each'
        from selclick.rb:5:in `<main>'
4

2 回答 2

1

爪哇

List<WebElement> elements = driver.find_elements( _locator_ );
for (i=0; i<elements.size(); i++) {
    List<WebElement> elements2 =driver.find_elements(_locator_) 

    WebElement ele = elements2.get(i);

    ele.click();
}
于 2012-06-28T16:12:03.570 回答
1

我确实相信这是“按预期工作”。

您的循环的第一次迭代工作正常,然后每个迭代都失败了。原因正是由错误给出的。由于页面已重新加载,对象引用不再指向正确的链接。

我不确定这是否清楚,所以让我们看看到底发生了什么。

您访问您的页面。所有链接都收集在数组中。在循环的第一次迭代中,单击第一个链接。您访问的是同一个页面的链接。您不再位于以前的页面上。循环的第二次迭代查找前一页的链接。它不再存在,因为您在下一页。因此,错误。

你到底想要完成什么?您是否尝试访问网站上的每个页面?

如果您尝试抓取该网站,我可以推荐 Mechanize 或 Anemone 吗?

于 2012-04-27T07:20:20.397 回答