Akash Panchal
Akash Panchal

Reputation: 305

Clicking all links and open all clicked links in new tabs using Selenium Ruby

I want to open all clicked links in new browser tabs from the page I load using driver.get "http://www.example.com". I have got answer in other question I asked before but it is not working as I expected. Code is following:

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 }

After running this code Selenium Web Driver opens Firefox, loads requested page and clicks on first anchor tag it founds and after that it crashes with error.

I have also tried this one driver.find_element(:tag_name, "a").send_keys [:control,'t'] this one opens new tabs but without loading pages, blank tabs.

How can I arrange this in loop so I wont get error and all clicked links on first loaded page will open in separate browser tabs?

Do I need to use wait for this to work?

[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/[email protected]/components/command_processor.js:9069:in `unknown'
        from [remote server] file:///C:/Users/AKASH/AppData/Local/Temp/webdriver-profile20120427-1496-sev0j4/extensions/[email protected]/components/command_processor.js:9504:in `unknown'
        from [remote server] file:///C:/Users/AKASH/AppData/Local/Temp/webdriver-profile20120427-1496-sev0j4/extensions/[email protected]/components/command_processor.js:10510:in `unknown'
        from [remote server] file:///C:/Users/AKASH/AppData/Local/Temp/webdriver-profile20120427-1496-sev0j4/extensions/[email protected]/components/command_processor.js:10529:in `unknown'
        from [remote server] file:///C:/Users/AKASH/AppData/Local/Temp/webdriver-profile20120427-1496-sev0j4/extensions/[email protected]/components/command_processor.js:10534:in `unknown'
        from [remote server] file:///C:/Users/AKASH/AppData/Local/Temp/webdriver-profile20120427-1496-sev0j4/extensions/[email protected]/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>'

Upvotes: 1

Views: 1968

Answers (2)

coldashes
coldashes

Reputation: 11

Java

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();
}

Upvotes: 1

sunnyrjuneja
sunnyrjuneja

Reputation: 6123

I do believe this is "working as intended."

The first iteration of your loop is working correctly and then each one after is failing. The reason is exactly given by the error. Since the page has reloaded, object references are no longer pointing to the correct link.

I'm not sure if this is clear so lets follow whats happening exactly.

You visit your page. All the links are collected in array. On the first iteration of your loop, you click on your first link. You visit that link which is just the same page. You are no longer on the page you were before. The second iteration of the loop looks for the previous page's link. It is no longer there because you're on the next page. Thus, the error.

What is it exactly you're trying to accomplish? Are you trying to visit every page on the site?

If you're attempting trying to crawl the site, may I recommend Mechanize or Anemone instead?

Upvotes: 1

Related Questions