Switching to a new window opened by an existing IE COM object

We are trying to automate a functionality test of a web app that we manage.  The app starts with a splash page that has an "accept" button on it.  Clicking the accept button launches a new IE window with the next page of the app and attempts to close the existing IE window.

To do this, we're using an InternetExplorer.Application COM object.

We've figured out how to do everything we need on the initial splash page, and we can utilize the next page just fine when we open it directly in the script.

The problems have come up when we attempt to test the functionality of that "Accept" button.

1) IE is throwing the popup box saying that the webpage is trying to close the window.  Try as we might, we cannot figure out how to respond "Yes" to that prompt. (I've tried various derivations of SendKeys with no success, but it's possible I just wasn't using it appropriately.)

2) After manually clicking "Yes", the new window loads, but we can no longer control it with the existing COM object.  This makes sense, since the existing COM object was associated with the IE window that just closed, but is there any way to "Get" or "Capture" the new IE window as an existing COM object so that we can just continue working with it?

Thanks in advance!

October 31st, 2012 8:37pm

1) IE is throwing the popup box saying that the webpage is trying to close the window.  Try as we might, we cannot figure out how to respond "Yes" to that prompt. (I've tried various derivations of SendKeys with no success, but it's possible I just wasn't using it appropriately.)

The dialog box that says "The webpage you are using is trying to close this window. Do you want to close this window?", AFAIK, is a security feature. I doubt it can be disabled.

2) After manually clicking "Yes", the new window loads, but we can no longer control it with the existing COM object.  This makes sense, since the existing COM object was associated with the IE window that just closed, but is there any way to "Get" or "Capture" the new IE window as an existing COM object so that we can just continue working with it?

I do not think that is possible.

Bill

Free Windows Admin Tool Kit Click here and download it now
October 31st, 2012 8:44pm

Actually, Bill, that may not be accurate.  You are right that nothing can be done to release the box once it pops up.  And, without seeing the controlling script it's hard to be too specific, but it should be possible to get the window to close properly if the script declares itself  the "opener" with a value of "Me".  I've been able to do that successfully in the past.  The opener property is part of IE's DOM.  For example, assuming the IE object in the script is called oIE, a VBS example looks like this ...

  with oIE.document.parentWindow
    .opener="Me"
    .close ' Close the window
  end with

Regarding the capture of an existing IE window, it can be done using the Shell.Application object.  For example, assuming the title of the target page is known, a VBS example function would look something like this ...

Function FindIE(sTitle, obj)
dim w, bFound
  with createobject("shell.application")
    bFound = false
    for each w in .windows
      if instr(lcase(typename(w.document)),"htmldocument") <> 0 then
        if w.document.title = sTitle then
          set obj = w : bFound = True : exit for
        end if
      end if
    next
  end with
  FindIE = bFound
end function

The function returns True if it finds the named window and false if it doesn't.  The window object is returned as the second argument of the function, when successful.  I've used this any number of time in scripts.

HTH,

October 31st, 2012 9:42pm

Cool.

Bill

Free Windows Admin Tool Kit Click here and download it now
October 31st, 2012 9:43pm

Thanks Tom!

The rest of the script is already written in powershell, so I tried converting your ideas over to that.

This is what I tried, but it only resulted in a second "the webpage you are viewing is trying to close the window" popup.

$ie = New-Object -ComObject InternetExplorer.Application
$ie.Visible = $true
$ie.Navigate("https://URLhere/")
$ie.Document.getElementById("Submit1").Click()
$ie.document.parentWindow.opener = "Me"
$ie.document.parentWindow.close()
I'm trying to figure out how to translate your shell.application section to Powershell now.  I'll post again if I have success with that.  (Unless someone wants to fill me in.)

November 1st, 2012 4:00pm

Ok, figured out how to snag control of the new IE window, courtesy of Shay Levi.

$app = new-object -com shell.application

# get a list of all IE windows, filter to the one you need by matching its LocationURL
$ie = $app.windows() | where {$_.Type  -eq "HTMLDocument" -and $_.LocationURL -match "yourCriteria"}

# navigate away
$ie.navigate("http://www.google.com")   

  • Marked as answer by Jester4kicks Monday, November 05, 2012 4:34 PM
Free Windows Admin Tool Kit Click here and download it now
November 1st, 2012 4:13pm

I think you're going to have problems bypassing the "script wants to close this web page. do you want to allow this?" message for a web page unless you edit the script for that page.

Bill

  • Marked as answer by Jester4kicks Monday, November 05, 2012 4:35 PM
November 3rd, 2012 1:44pm

Yeah, I think you're right, Bill.  I searched high and low, tried a few different commands, methods, IE security zone settings... all sorts of things.  Without modifying the webpage itself, there doesn't appear to be a way to dodge that popup.

We resorted to using a SendKeys command to respond to the prompt, but I REALLY hate resorting to that.  Does anyone have any better way of acknowledging that prompt via a more direct script command?

Free Windows Admin Tool Kit Click here and download it now
November 3rd, 2012 2:13pm

Probably not, as this was added to browsers as a security measure. If there were a bypass feature, all rogue code would use it, rendering it useless.

Bill

November 3rd, 2012 2:33pm

Yeah, I was afraid of that.

I'll leave the thread open for one last shot... can anyone think of any better way to acknowledge that prompt than with a SendKeys command?

Free Windows Admin Tool Kit Click here and download it now
November 5th, 2012 3:12pm

No, I'm afraid that if you insist on going that route, doing something like what you're doing (trying to send a keystroke) is about your only option. I don't recommend it, though, as SendKeys can be quite brittle. I never recommend SendKeys in any kind of production script.

Bill

November 5th, 2012 3:47pm

Thanks, Bill.

I've marked Tom's response as correct for the second part of it, Shay's script for the powershell version of it, and you correct assertion that there's no way to effectively get around the "close" popup without rewriting the page itself.

Thanks all!

Free Windows Admin Tool Kit Click here and download it now
November 5th, 2012 4:37pm

OK, I was away so I couldn't follow up.  I think I've found a solution.  Try this ...

Dim oIE
  
if FindIE("Hotmail", oIE) then 
'wsh.echo "Found"
  oIE.quit
end if
wsh.echo "Done"
  
Function FindIE(sTitle, obj)
dim w, bFound
  with createobject("shell.application")
    bFound = false
    for each w in .windows
      if instr(lcase(typename(w.document)),"htmldocument") <> 0 then
        if instr(lcase(w.document.title), lcase(sTitle)) then 
          set obj = w : bFound = True : exit for
        end if
      end if
    next
  end with
  FindIE = bFound
end function

It just Quits the instance of IE that it finds that matches the supplied title.  Seemed to work for me in IE8 and Win7.
November 5th, 2012 7:17pm

This is fantastic.  Works great.  
Free Windows Admin Tool Kit Click here and download it now
August 24th, 2015 6:23pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics