1

I am using Selenium on Java to auto test a webpage, this page's script calls window.close() right after page is loaded.

            window.opener='whatever';
            window.open('','_parent','');
            window.close();

In IE9, it pops up a confirm dialog "The webpage you are viewing is trying to close the window disable" with Yes/No options. Pressing Yes will close the webpage.

I don't want this closing (to keep the session) and neither the popup. Could you please suggest me a solution for ignore or override the window.close() script.

Thanks in advance.

Antony Dao
  • 425
  • 2
  • 14

1 Answers1

1

You can simply overwrite the window.close function with JavaScript.

String disableCloseScript = "window.close = function() { }";
((JavascriptExecutor)driver).executeScript(disableCloseScript);

Checkout these answers for more information:

Community
  • 1
  • 1
0xcaff
  • 13,085
  • 5
  • 47
  • 55
  • Thanks for your answer. I tried override the `window.close()` method by that way and it works on Chrome, not work on IE9. `window.close = function(){ alert("hello"); }; window.opener='whatever'; window.open('','_parent',''); window.close();` On Chrome, it shows the alert `hello`, but on IE9, it closes the webpage tab – Antony Dao Dec 23 '16 at 05:57
  • Damn IE. Is the call to close in a function? If it is, you could try proxying the window object and ignore the `close()` call or regexp replace the call out. – 0xcaff Dec 23 '16 at 06:17