I'm trying to automate a browser process using Selenium WebDriver.
The web site generates a series of downloadable files with a link button for each. Clicking on the link button opens up a new browser window in which the file is downloaded. But the new browser window is not coded to close.
I have located the JavaScript/jQuery function OpenWindowUrl in chrome's web dev tool that opens up the new browser window, and the gist of the function is shown below.
As you can see, the function calls another function GetRootWin to open up the new browser window and download the file, then forces the new browser window into the foreground. I wish it had a few more lines to close it after 3 seconds or so.
Since the variable wnd which is the handle of the new browser window is scoped inside the function definition, I'm not able to use it in my automation to close the browser window like wnd.close();, because wnd is undefined outside of the function.
Although my automation process works fine without closing the child windows, potentially it will leave me over 200 child windows open, not a good idea.
Is there a way to override this OpenWindowUrl function such that my new OpenWindowUrl will be called by the website instead? If yes, I can run my new snippet for OpenWindowUrl using Selenium's IJavaScriptExecutor before downloading the files. Or is there a way to inject wnd.close(); into the OpenWindowUrl function?
$.extend(Namespace('NS'), {
OpenWindowUrl: function (url, winName, parameters) {
... ...
var wnd = NS.GetRootWin().open(url, winName, parameters);
// Force the window into the foreground
setTimeout(function () {
wnd.focus();
}, 500);
},
});