# Closing popups watir-webdriver

**URL:** <https://www.ruby-forum.com/t/closing-popups-watir-webdriver/224328>\
**Category:** Ruby\
**Created:** [November 7, 2012, 3:32pm UTC](https://www.ruby-forum.com/t/closing-popups-watir-webdriver/224328 "2012-11-07T15:32:08Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [November 7, 2012, 3:32pm UTC](https://www.ruby-forum.com/t/closing-popups-watir-webdriver/224328/1 "2012-11-07T15:32:08Z")

</div>

Sometimes popup comes when a link is clicked, sometimes popup comes when  
the browser is closed…

So it happens sometimes in IE that if I close the browser, the popup  
remains there. How to ensure that any related popup with a browser  
should get close when am closing the browser.

---

<div class="post-metadata">

**Author:** ![proggrammer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/47e85d/32.png) [@proggrammer](https://www.ruby-forum.com/u/proggrammer)\
**Post date:** [November 7, 2012, 7:59pm UTC](https://www.ruby-forum.com/t/closing-popups-watir-webdriver/224328/2 "2012-11-07T19:59:27Z")

</div>

[http://watirwebdriver.com/browser-popups/](http://watirwebdriver.com/browser-popups/)

---

<div class="post-metadata">

**Author:** ![proggrammer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/47e85d/32.png) [@proggrammer](https://www.ruby-forum.com/u/proggrammer)\
**Post date:** [November 8, 2012, 8:01am UTC](https://www.ruby-forum.com/t/closing-popups-watir-webdriver/224328/3 "2012-11-08T08:01:48Z")

</div>

Joel P. wrote in post #1083436:

> [http://watirwebdriver.com/browser-popups/](http://watirwebdriver.com/browser-popups/)

This works when the main window is not closed, but sometimes when you  
close a window, the window is closed but a pop-up comes.

If I use the code given I get error:

Errno::ECONNREFUSED: No connection could be made because the target  
machine actively refused  
it. - connect(2)  
from C:/Ruby187/lib/ruby/1.8/net/http.rb:560:in `initialize' from C:/Ruby187/lib/ruby/1.8/net/http.rb:560:in `open’  
from C:/Ruby187/lib/ruby/1.8/net/http.rb:560:in `connect' from C:/Ruby187/lib/ruby/1.8/timeout.rb:53:in `timeout’  
from C:/Ruby187/lib/ruby/1.8/timeout.rb:101:in `timeout' from C:/Ruby187/lib/ruby/1.8/net/http.rb:560:in `connect’  
from C:/Ruby187/lib/ruby/1.8/net/http.rb:553:in `do_start' from C:/Ruby187/lib/ruby/1.8/net/http.rb:542:in `start’  
from C:/Ruby187/lib/ruby/1.8/net/http.rb:1035:in `request' from C:/Ruby187/lib/ruby/gems/1.8/gems/selenium-webdriver-2.25.0/lib/selenium/webdriv er/remote/http/default.rb:82:in `response\_for’  
from  
C:/Ruby187/lib/ruby/gems/1.8/gems/selenium-webdriver-2.25.0/lib/selenium/webdriv  
er/remote/http/default.rb:38:in `request' from C:/Ruby187/lib/ruby/gems/1.8/gems/selenium-webdriver-2.25.0/lib/selenium/webdriv er/remote/http/common.rb:40:in `call’  
from  
C:/Ruby187/lib/ruby/gems/1.8/gems/selenium-webdriver-2.25.0/lib/selenium/webdriv  
er/remote/bridge.rb:598:in `raw_execute' from C:/Ruby187/lib/ruby/gems/1.8/gems/selenium-webdriver-2.25.0/lib/selenium/webdriv er/remote/bridge.rb:576:in `execute’  
from  
C:/Ruby187/lib/ruby/gems/1.8/gems/selenium-webdriver-2.25.0/lib/selenium/webdriv  
er/remote/bridge.rb:207:in `getWindowHandles' from C:/Ruby187/lib/ruby/gems/1.8/gems/selenium-webdriver-2.25.0/lib/selenium/webdriv er/common/driver.rb:185:in `window\_handles’  
from  
C:/Ruby187/lib/ruby/gems/1.8/gems/watir-webdriver-0.6.1/lib/watir-webdriver/wind  
ow.rb:119:in `locate' from C:/Ruby187/lib/ruby/gems/1.8/gems/watir-webdriver-0.6.1/lib/watir-webdriver/wind ow.rb:106:in `handle’  
from  
C:/Ruby187/lib/ruby/gems/1.8/gems/watir-webdriver-0.6.1/lib/watir-webdriver/wind  
ow.rb:99:in `use’

---

<div class="post-metadata">

**Author:** ![proggrammer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/47e85d/32.png) [@proggrammer](https://www.ruby-forum.com/u/proggrammer)\
**Post date:** [November 8, 2012, 10:48am UTC](https://www.ruby-forum.com/t/closing-popups-watir-webdriver/224328/4 "2012-11-08T10:48:18Z")

</div>

I think your best bet is probably to loop through the active windows and  
close each, something like this:

browser.windows.each {|w| w.close rescue nil}  
browser.close rescue nil #The previous line should do this, but it’s  
best to be sure

If you run that at the end it should close all webdriver windows.

---

<div class="post-metadata">

**Author:** ![proggrammer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/47e85d/32.png) [@proggrammer](https://www.ruby-forum.com/u/proggrammer)\
**Post date:** [November 8, 2012, 11:26am UTC](https://www.ruby-forum.com/t/closing-popups-watir-webdriver/224328/5 "2012-11-08T11:26:37Z")

</div>

Joel P. wrote in post #1083530:

> I think your best bet is probably to loop through the active windows and  
> close each, something like this:
> 
> browser.windows.each {|w| w.close rescue nil}  
> browser.close rescue nil #The previous line should do this, but it’s  
> best to be sure
> 
> If you run that at the end it should close all webdriver windows.

What is this: browser??  
I am sure its not the current browser? then what is it?

---

<div class="post-metadata">

**Author:** ![proggrammer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/47e85d/32.png) [@proggrammer](https://www.ruby-forum.com/u/proggrammer)\
**Post date:** [November 8, 2012, 1:30pm UTC](https://www.ruby-forum.com/t/closing-popups-watir-webdriver/224328/6 "2012-11-08T13:30:08Z")

</div>

browser or driver is the most common variable name for a webdriver  
session. Since you didn’t provide examples of your code I used the  
default notation.

---

<div class="post-metadata">

**Author:** ![proggrammer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/47e85d/32.png) [@proggrammer](https://www.ruby-forum.com/u/proggrammer)\
**Post date:** [November 8, 2012, 1:41pm UTC](https://www.ruby-forum.com/t/closing-popups-watir-webdriver/224328/7 "2012-11-08T13:41:27Z")

</div>

Joel P. wrote in post #1083550:

> browser or driver is the most common variable name for a webdriver  
> session. Since you didn’t provide examples of your code I used the  
> default notation.

so suppose :

br=Watir::Browser.new :ie  
br.goto(url)  
br.close  
#here the popup comes  
br.windows.each {|w| w.close rescue nil}

* * *

here br is not an array, and its also closed, the problem is the next  
popup, which has no know handle.

Am I write using br here?

---

<div class="post-metadata">

**Author:** ![proggrammer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/47e85d/32.png) [@proggrammer](https://www.ruby-forum.com/u/proggrammer)\
**Post date:** [November 8, 2012, 4:57pm UTC](https://www.ruby-forum.com/t/closing-popups-watir-webdriver/224328/8 "2012-11-08T16:57:03Z")

</div>

what does this line do?

> br.execute\_script(“window.onbeforeunload = null”)

---

<div class="post-metadata">

**Author:** ![proggrammer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/47e85d/32.png) [@proggrammer](https://www.ruby-forum.com/u/proggrammer)\
**Post date:** [November 8, 2012, 4:18pm UTC](https://www.ruby-forum.com/t/closing-popups-watir-webdriver/224328/9 "2012-11-08T16:18:45Z")

</div>

Try it this way around

br=Watir::Browser.new :ie  
br.goto(url)  
br.windows.each {|w| w.close rescue nil}  
br.close rescue nil

If you still get popups and they’re triggered by JavaScript when leaving  
the page, you might be able to do this before closing:  
br.execute\_script(“window.onbeforeunload = null”)

---

<div class="post-metadata">

**Author:** ![proggrammer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/47e85d/32.png) [@proggrammer](https://www.ruby-forum.com/u/proggrammer)\
**Post date:** [November 9, 2012, 7:07am UTC](https://www.ruby-forum.com/t/closing-popups-watir-webdriver/224328/10 "2012-11-09T07:07:00Z")

</div>

Joel P. wrote in post #1083623:

> It overwrites the JavaScript function on the current page which triggers  
> when you try to leave the page.  
> If the popups are appearing when you’re closing the browser, executing  
> that bit of code before closing the browser should prevent popups from  
> appearing in the first place, assuming it’s JavaScript that’s doing  
> that.
> 
> I got it from this page: [http://watirwebdriver.com/javascript-dialogs/](http://watirwebdriver.com/javascript-dialogs/)  
> I’ve used it myself on a few pages which trigger events when you try to  
> close them, works like a charm 🙂

Not working!!  
Now I know people will say that it is not triggered by javascript.. then  
how it comes who triggers it?

---

<div class="post-metadata">

**Author:** ![proggrammer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/47e85d/32.png) [@proggrammer](https://www.ruby-forum.com/u/proggrammer)\
**Post date:** [November 9, 2012, 10:30am UTC](https://www.ruby-forum.com/t/closing-popups-watir-webdriver/224328/11 "2012-11-09T10:30:31Z")

</div>

Maybe you could try looking at the source code on the page which is  
triggering the popup. It might be a different trigger.

Regardless, provided each window was spawned from this webdriver session  
then the windows close loop should get them all. As long as you do it  
before terminating the webdriver session itself.

---

<div class="post-metadata">

**Author:** ![proggrammer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/47e85d/32.png) [@proggrammer](https://www.ruby-forum.com/u/proggrammer)\
**Post date:** [November 8, 2012, 9:08pm UTC](https://www.ruby-forum.com/t/closing-popups-watir-webdriver/224328/12 "2012-11-08T21:08:42Z")

</div>

It overwrites the JavaScript function on the current page which triggers  
when you try to leave the page.  
If the popups are appearing when you’re closing the browser, executing  
that bit of code before closing the browser should prevent popups from  
appearing in the first place, assuming it’s JavaScript that’s doing  
that.

I got it from this page: [Loading...](http://watirwebdriver.com/javascript-dialogs/)  
I’ve used it myself on a few pages which trigger events when you try to  
close them, works like a charm 🙂

---

<div class="post-metadata">

**Author:** ![proggrammer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/47e85d/32.png) [@proggrammer](https://www.ruby-forum.com/u/proggrammer)\
**Post date:** [November 9, 2012, 1:51pm UTC](https://www.ruby-forum.com/t/closing-popups-watir-webdriver/224328/13 "2012-11-09T13:51:44Z")

</div>

I have confirmed… it is javascript indeed. anyway to catch the issue?

---

<div class="post-metadata">

**Author:** ![proggrammer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/47e85d/32.png) [@proggrammer](https://www.ruby-forum.com/u/proggrammer)\
**Post date:** [November 9, 2012, 8:09pm UTC](https://www.ruby-forum.com/t/closing-popups-watir-webdriver/224328/14 "2012-11-09T20:09:58Z")

</div>

Are you running that JS overwrite as the last thing before closing the  
page? If you do it but then refresh the page, your changes will be lost.  
You’d need to make it the penultimate command:

br.goto “[ᐈ&nbsp;eSports News & Tricks (2023) 🥇 ENEWS.GG](http://www.4guysfromrolla.com/demos/OnBeforeUnloadDemo1.htm)”  
br.execute\_script(“window.onbeforeunload = null”)  
br.close rescue nil

The above works for me, does it work for you? If that isn’t working then  
can you post the JavaScript code from the page, and the code you’re  
using which isn’t working? It might be a variant on that function, like  
“window.onunload” that you need to overwrite.
