2

I've written a Ruby script to automate some user operations using IE. I'm using Selenium Web Driver for IE. Below is my code.

require 'selenium-webdriver'

browser = Selenium::WebDriver.for :ie

first_window = browser.window_handle
browser.switch_to.frame(browser.find_element(:id=> 'outerFrame'))
browser.switch_to.frame(browser.find_element(:id=> 'innerFrame'))

table_rows = browser.find_element(:id=> 'AllItems').find_element(:tag_name=> 'table').find_elements(:tag_name=> 'tr')
count_cell = table_rows.at(table_rows.length-1).find_elements(:tag_name=> 'td').at(1).find_element(:tag_name=> 'a')
count_cell.click

sleep(5)

all_windows = browser.window_handles
new_window = browser.window_handles.last

browser.switch_to.window(new_window)

btn = browser.find_element(:id=> 'btn_export')
btn.click

At one point, after clicking a button, a new page is opened. Now, when I try to switch to the new window, I get the following error.

C:/Ruby21/lib/ruby/2.1.0/net/http.rb:879:in 'initialize': No connection could be made because the target machine actively refused it. - connect(2) for "127.0.0.1" port 5555 (Errno::ECONNREFUSED) from C:/Ruby21/lib/ruby/2.1.0/net/http.rb:879:in 'open' from C:/Ruby21/lib/ruby/2.1.0/net/http.rb:879:in 'block in connect' from C:/Ruby21/lib/ruby/2.1.0/timeout.rb:75:in 'timeout' from C:/Ruby21/lib/ruby/2.1.0/net/http.rb:878:in 'connect' from C:/Ruby21/lib/ruby/2.1.0/net/http.rb:863:in 'do_start' from C:/Ruby21/lib/ruby/2.1.0/net/http.rb:852:in 'start' from C:/Ruby21/lib/ruby/2.1.0/net/http.rb:1375:in 'request' from C:/Ruby21/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.48.1/lib/selenium/webdriver/remote/http/default.rb:107:in 'response_for' from C:/Ruby21/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.48.1/lib/selenium/webdriver/remote/http/default.rb:58:in 'request' from C:/Ruby21/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.48.1/lib/selenium/webdriver/remote/http/common.rb:59:in 'call' from C:/Ruby21/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.48.1/lib/selenium/webdriver/remote/bridge.rb:664:in 'raw_execute' from C:/Ruby21/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.48.1/lib/selenium/webdriver/remote/bridge.rb:642:in 'execute' from C:/Ruby21/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.48.1/lib/selenium/webdriver/remote/bridge.rb:216:in 'switchToWindow' from C:/Ruby21/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.48.1/lib/selenium/webdriver/common/target_locator.rb:81:in 'window' from script.rb:18:in ''

I tried my bit to resolve the error, by modifying firewall and added rules for port 5555. Even then issue persists. I found some already exsting questions on this, but most of them are related to Java or .Net. How can I resolve this error? Can anyone point me in the right direction?

7
  • 1
    does the same thing happen on chrome or firefox? Commented Dec 7, 2015 at 23:24
  • The site is IE specific. It does not open on other browsers. Commented Dec 8, 2015 at 2:15
  • before clicking and going to new window save the wnidow handle of 1st webpage then click and after your operations are done use saved window handle to come back to original screen Commented Dec 9, 2015 at 12:33
  • I am doing the same currently. "count_cell.click" is the step, where it opens a new page. Commented Dec 9, 2015 at 12:40
  • If you could provide a bit of html/css/js to reproduce the issue it might help us debug. The most simple example that can reproduce it. Commented Dec 9, 2015 at 22:14

2 Answers 2

0

I am not a ruby expert, but I have seen similar problems happening. I have a Python-ic solution. All you need to do is Ruby-fy the code. https://gist.github.com/ab9-er/08c3ce7d2d5cdfa94bc7

def change_window(browser):
    """
    Simple window switcher without the need of playing with ids.
    @param browser: Current browser instance
    """
    curr = browser.current_window_handle
    all_handles = browser.window_handles
    for handle in list(set([curr]) - set(all_handles)):
        return browser.switch_to_window(handle)
Sign up to request clarification or add additional context in comments.

1 Comment

For what it is worth, the Ruby code he is using is the appropriate method for switching to a window in Ruby.
0

Try switching back to the top level browsing context before switching to a new window.

browser.switch_to.default_content
current_window = browser.window_handle
new_window = browser.window_handles.find { |win| win != current_window }
browser.switch_to.window(new_window)

It should do this implicitly, so if it isn't, it's likely a bug. If this works, please let me know so I can see if we need to file a bug report.

2 Comments

Can you verify that there are more than one windows open? And can you verify this code works with Firefox or Chrome?
Also, I was reminded that the window handles aren't returned with any order, so I modified the code I provided.

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.