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?

dr_dev
  • 492
  • 3
  • 17
  • 1
    does the same thing happen on chrome or firefox? – Mesut GUNES Dec 07 '15 at 23:24
  • The site is IE specific. It does not open on other browsers. – dr_dev Dec 08 '15 at 02: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 – Umesh Kumar Dec 09 '15 at 12:33
  • I am doing the same currently. "count_cell.click" is the step, where it opens a new page. – dr_dev Dec 09 '15 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. – snowe Dec 09 '15 at 22:14
  • This might help you: http://stackoverflow.com/questions/18484161/unable-to-run-selenium-script-on-ie – 13aal Dec 11 '15 at 13:18
  • did you try [this approach](http://elementalselenium.com/tips/4-work-with-multiple-windows)? – timbre timbre Dec 11 '15 at 23:46

2 Answers2

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)
Abhinav
  • 992
  • 2
  • 11
  • 26
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.

titusfortner
  • 4,099
  • 2
  • 15
  • 29