0

Hello I'm using Internet Explorer headless mode to perform automated tasks on my script but I'm encountering an issue when sometimes by surfing on some pages there are authentication popups that show up to log into the page but not through a HTML popup but more of a windows popup.

I wanted to get control of the popup so when it pops up it will be visible on TOP. Right now it's very random it sometimes shows on top of all programs but sometimes it's far in the background and then the automation fails because the user didn't see the popup.

Hardcoding username and password is not an option.

Does anybody know how I can manipulate the popup (see capture in attachment) and eventually atleast put it on top of screen?

Thanks!

Windows Security Popup

iThomas
  • 15
  • 4
  • You can detect the opening of a Dialog box using UI Automation, setting an event handler (`Automation.AddAutomationEventHandler`) using the `WindowPattern.WindowOpenedEvent`, starting from the `AutomationElement.RootElement` (the Desktop), as described [here](https://stackoverflow.com/a/55028688/7444103). Sample VB.Net code, doing ~the same thing, [here](https://stackoverflow.com/a/51505218/7444103). – Jimi Jan 23 '20 at 09:46
  • When you get the handle of the Dialog (it's set in the properties of the `AutomationEventArgs` object of the event handler), you can use [SetWindowPos](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowpos) or [SetForegroundWindow](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setforegroundwindow) to move it to the foreground, since the `WindowPatter.SetWindowVisualState` method may not work as intended with this kind of dialogs. – Jimi Jan 23 '20 at 09:59
  • @Jimi thanks for your input it wasn't easy to understand but I got the hang of it how it can work although I wasn't able to implement [SetForegroundWindow](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setforegroundwindow) so instead I used [AppActivate](https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.interaction.appactivate?view=netframework-4.8) – iThomas Jan 23 '20 at 15:06

1 Answers1

0

Solution:

Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent, AutomationElement.RootElement,
           TreeScope.Subtree,
           Sub(UIElm, evt)
           Dim element As AutomationElement = TryCast(UIElm, AutomationElement)
           If element Is Nothing Then Return
              Dim NativeHandle As IntPtr = CType(element.Current.NativeWindowHandle, IntPtr)
              If InStr(element.Current.Name, "Windows Security") > 0 Then
                    AppActivate(element.Current.ProcessId)
              End If

        End Sub)
    End Sub
iThomas
  • 15
  • 4