Closing popups watir-webdriver

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.

http://watirwebdriver.com/browser-popups/

Joel P. wrote in post #1083436:

Loading...

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’

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.

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?

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.

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?

what does this line do?

br.execute_script(“window.onbeforeunload = null”)

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”)

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/
I’ve used it myself on a few pages which trigger events when you try to
close them, works like a charm :slight_smile:

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

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.

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...
I’ve used it myself on a few pages which trigger events when you try to
close them, works like a charm :slight_smile:

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

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 “ᐈ eSports News & Tricks (2023) 🥇 ENEWS.GG
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.