Jump to content

_IENavigate_kill_popup function


Recommended Posts

I use a lot of IE functions in my scripts and find it really annoying when I try to use _IENavigate to leave a webpage and I get popups saying things like "Are you sure you want to leave this page?". I searched the forum and could not find a solution that I liked so I created this function. I hope others find it useful and as always I am open to any constructive criticism (that's how we learn). :D

_ProcessGetParent() function taken from MrCreatoR's post (thanks MrCreatoR)

#include <IE.au3>

;This page currently has displays a popup when you try to navigate away from the page
$any_url = "aquaponics4you.com"
;$any_url = "yahoo.com"

$oIE = _IECreate($any_url)
$oIE = _IENavigate_kill_popup($oIE, "Google.com")

Func _IENavigate_kill_popup($IEObject, $URL, $visible = 1, $wait = 1)
    If Not IsObj($IEObject) Then Return SetError(1, 0, 0)
    $closed_process = False

    ;gets the IE process
    $pid = WinGetProcess(_IEPropertyGet($IEObject, "hwnd"))

    ;Trys to navigate to URL
    _IENavigate($IEObject, $URL, 0)

    ;Gets list of all process
    $aProc_List = ProcessList()

    ;Checks to see if any process were opened by the IE window. If process where opened it closes them and the IE window.
    For $i = 1 To $aProc_List[0][0]
        If _ProcessGetParent($aProc_List[$i][1]) = $pid Then
            ProcessClose($aProc_List[$i][1])
            $closed_process = True
        EndIf
    Next

    ;will create a new IE with old IE object varable if it no longer exists.
    If $closed_process = True Then
        $IEObject = _IECreate($URL, 0, $wait)
        Return $IEObject
    Else
        If $wait = 1 Then _IELoadWait($IEObject)
        Return $IEObject
    EndIf
EndFunc   ;==>_IENavigate_stop_pop

Func _ProcessGetParent($iPID)
    Local $wbemFlagReturnImmediately = 0x10
    Local $wbemFlagForwardOnly = 0x20
    Local $colItems = ""
    Local $strComputer = "localhost"

    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Process", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

    If IsObj($colItems) Then
        For $objItem In $colItems
            If $objItem.ProcessID = $iPID Then Return $objItem.ParentProcessID
        Next
    EndIf

    Return SetError(1, 0, 0)
EndFunc   ;==>_ProcessGetParent
Edited by Herb191
Link to post
Share on other sites

I found some errors in my code so I changed the function so that it would work right.

#include <IE.au3>

;This page currently has displays a popup when you try to navigate away from the page
$any_url = "http://aquaponics4you.com/"
;$any_url = "http://www.yahoo.com/"

$oIE = _IECreate($any_url)
$oIE = _IENavigate_kill_popup($oIE, "http://www.google.com/")

Func _IENavigate_kill_popup($IEObject, $URL, $visible = 1)
    If Not IsObj($IEObject) Then Return SetError(1, 0, 0)
    $closed_process = False

    $list_of_windows = WinList()

    ;Trys to navigate to URL
    _IENavigate($IEObject, $URL, 0)
    _IELoadWait($IEObject, 0, 5000)

    If _IEPropertyGet($IEObject, "locationurl") <> $URL Then
        $closed_process = True
        $hwnd = _IEPropertyGet($IEObject, "hwnd")

        Do ;will loop until the IE window can close
            If WinClose($hwnd) = 1 And WinExists($hwnd) = 1 Then
                $list_of_windows2 = WinList()
                For $i = 1 To $list_of_windows2[0][0]
                    For $i2 = 1 To $list_of_windows[0][0]
                        $win_existed_before = False
                        If $list_of_windows2[$i][1] = $list_of_windows[$i2][1] Then
                            $win_existed_before = True
                            ExitLoop
                        EndIf
                    Next
                    ;if the window did not exist before it will close it
                    If $win_existed_before = False Then WinClose($list_of_windows2[$i][1])
                Next
            EndIf
        Until WinClose($hwnd) = 0
    EndIf

    ;will create a new IE object if old IE object made popups.
    If $closed_process = True Then
        $IEObject = _IECreate($URL, 0)
        Return $IEObject
    Else
        _IELoadWait($IEObject)
        Return $IEObject
    EndIf
EndFunc   ;==>_IENavigate_kill_popup
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By yffulf
      Is there probably a way to click ie popup button and hide or invisible popup button in the same time?
      I try to use WinSetState @SW_hide ,
      the result is ie crashing or no response because button hide and the next click command won't work...
      $oForm=_IEFormGetObjByName($oIE,"form1") $obutton=_IEFormElementGetObjByName($oForm,"btn_OK") $hWnd = _IEPropertyGet($oIE, "hwnd") _IEAction($obutton,"focus") ControlSend($hWnd, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]", "{Enter}") WinWait("", "確定存檔嗎?") WinSetState("","確定存檔嗎?",@SW_Hide) ControlClick("", "確定存檔嗎?", "[CLASS:Button; TEXT:確定; Instance:1;]")  
    • By Rhidlor
      First off, the project I'm working on revolves around AS/400 "Client Access software", it's foundation is directly influenced by the thread linked at the bottom of this post. Moving on, to explain the problem I'm facing; my project utilizes an infinite While loop and automatically performs semi-hard coded monotonous tasks to save users time and effort, the problem is, occasionally and unexpectedly "Display Messages" will popup and the core script will continue executing instead of dismissing said message and the script "breaks". As a countermeasure I've added some code to the While loop in an effort to intervene and dismiss these display messages before the core script has a chance to do anything... however it doesn't work. I hope I didn't do too bad of a job explaining that. 
      So I think my question is: How can I temporarily "pause" the core script when these messages spontaneously appear?
      The only other solution I thought of would be to check if a display message has appeared before executing every line of code but that obviously isn't very practical.
      Any and all help is greatly appreciated!
      Thanks
       
      ;Loop to keep script running and handle display messages While 1 dismiss_display_messages() Sleep(50) WEnd ;Function to dismiss display messages Func dismiss_display_messages() If $ps.SearchText("Display Messages") Then $ps.SendKeys("[enter]", 8, 1) ;This while loop waits until the display message has disappeared to resume the core script While $ps.SearchText("Display Messages") Sleep(100) WEnd EndIf EndFunc  
       
    • By nooneclose
      I need to send a string of text to this popup and click on the ok button to save it.
      Here is the code I have so far:
      ;Start IE Sleep(7000) $oIE = _IECreate("http://www.google.com") Sleep(500) _IELoadWait($oIE) $hIE = _IEPropertyGet($oIE, "hwnd") ; Get Handle of the IE window Sleep(500) WinSetState($hIE, "", @SW_MAXIMIZE) ;Wait for a browser page load to complete Sleep(3000) _IENavigate($oIE, "https://properURL.com") Sleep(8000) _IELoadWait($oIE) ;Attach to a browser control embedded in another window $oIE = _IEAttach("https://"properURL.com", url") ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $oIE = ' & $oIE & @CRLF & '>Error code: ' & @error & ' Extended code: 0x' & Hex(@extended) & @CRLF) ;### Debug Console Sleep(2000) ;Get the title of the webpage ;Local $wTitle = _IEPropertyGet($oIE, "title") ;MsgBox($MB_SYSTEMMODAL, "Webpage title:", $wTitle) ;Clicks the new button Sleep(3000) _IEAction($nWorkOrderB, "focus") _IEAction($nWorkOrderB, "click") Sleep(5000) ;Store the Element names where the important data will be sent ;Store the long description button Local $wLongDButton = _IEGetObjById($oIE, "m65d795a4-img") ;Store the long Description field id Local $wComments = _IEGetObjById($oIE, "ma6499a9c-rte_iframe") ;Store the ok button id that is in the long description Local $wCommOk = _IEGetObjById($oIE, "m74031266-pb") ;******************************************************************************* ; Send the stored data to the proper field ;******************************************************************************* ;Click the long description button Sleep(300) _IEAction($wLongDButton, "focus") _IEAction($wLongDButton, "click") Sleep(300) ;Sends the Comments Sleep(500) _IEAction($wComments, "focus") _IEAction($wComments, "click") Sleep(500) _IEFormElementSetValue($wComments, "hello darkness my old friend") ;Click the ok button Sleep(500) _IEAction($wCommOk, "focus") _IEAction($wCommOk, "click") Sleep(500)  
      Here is the popup:

    • By AndyS19
      I'm trying to implement a Ctl-F popup box that looks something like the one that Notepad uses, but I'm not havine much luck.  I intend to get it working, then beef up the popup's contents to add several checkboxes, buttons and radio boxes.
      What my example code does is to use InputBox(), but that's not what I want.
      Here is my test code:
      #include <Array.au3> #include <GUIConstantsEx.au3> #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 Opt("GUICloseOnESC", 1) Opt("GUIOnEventMode", 1) Opt('MustDeclareVars', 1) OnAutoItExitRegister("ExitStageLeft") Opt("WinTitleMatchMode", -2) Global $hGUI _Main() Func _Main() $hGUI = GUICreate("Test ^F", 300, 200) setupSpecialKeysHandlers() GUISetOnEvent($GUI_EVENT_CLOSE, "Event_GUIClose") GUISetState() While (1) Sleep(157) WEnd EndFunc ;==>_Main Func handle_CTRL_F_key() Local $str $str = InputBox("Search", "Enter the string to search for:") ConsoleWrite("+++: $str ==>" & $str & "<==" & @CRLF) EndFunc ;==>handle_CTRL_F_key Func ExitStageLeft() Exit (99) EndFunc ;==>ExitStageLeft Func Event_GUIClose() Exit (1) EndFunc ;==>Event_GUIClose Func setupSpecialKeysHandlers() Local $ar, $parts, $key, $handler, $id Local $aAccelKeys[1][2] ; Create a table of Special keys and their handlers $ar = StringSplit("", "") _ArrayAdd($ar, "^f - handle_CTRL_F_key ") ReDim $aAccelKeys[UBound($ar) - 1][2] ; Now, create $aAccelKeys array with the table data. ; For each entry, create a Dummy GUI and associate its ; ID with the special key. For $ndx = 1 To UBound($ar) - 1 $parts = StringSplit($ar[$ndx], "-", 2) $key = StringStripWS($parts[0], 8) $handler = StringStripWS($parts[1], 8) $id = GUICtrlCreateDummy() $aAccelKeys[$ndx - 1][0] = $key $aAccelKeys[$ndx - 1][1] = $id GUICtrlSetOnEvent($id, $handler) Next GUISetAccelerators($aAccelKeys) ; Setup the Special keys hooks EndFunc ;==>setupSpecialKeysHandlers  
    • By help_me_please
      Hello forum world,
      I am having an issue with a popup window displaying after selecting something using the ControlCommand( ) function.
      I am able to select the desired item from the dropdown list using the control command function but a popup window opens immediately after, not allowing the control command function to finish.
      Such as:
      ConsoleWrite("Before ControlCommand" & @CRLF)
      ControlCommand($Win_Name, "", $ID, "SelectString", $DropItemName)
      ConsoleWrite("After ControlCommand" & @CRLF)
       
      ^^^^
      So in the console I can get the "Before...", then the ControlCommand executes the selection of the dropdown menu.  Popup window opens
      But since there is a popup, I can not get the "After..." statement to write to the console. 
      This is where I am stuck, as I can not execute another command to close the popup window.
       
      Send help, oh gracious autoit forum.  
×
×
  • Create New...