How can I use Autoit to close all javascript alerts and confirmations that may appear?
-
19-09-2019 - |
Question
I am testing in selenium and I have a bunch of stuff that happens on page load. I found out about autoit, and I was thinking that it could handle all javascript dialog issues. I was searching around the web for some autoit script that could handle this...
Other options would be helpful as well!
I don't necessarily need all the code, but some point in the right direction would be helpful.
Solution
Alright so this solution works for me... I was able to close 2 windows here. Also I will be able to handle popups based on their name which is cool. It always runs in the background after I have lauched the script. When I want it to end I simply close autoit from task manager or from the task bar area on the right.
While True
If WinWait('Name of your Popup', '', 1) == 1 Then
;Wait for page to load if found
Sleep(10000)
;Close Page
Send('!{F4}')
Sleep(5000)
;Confirm Dialog
Send('{ENTER}')
Sleep(1000)
;Close Lanucher Page
Send('!{F4}')
Sleep(5000)
;Confirm Dialog
Send('{ENTER}')
EndIf
;Let another thread have a turn
sleep(3)
WEnd
Also this is possible from directly within python, details: http://www.parrisstudios.com/?p=308, but you still need autoit3.
OTHER TIPS
If you are having issues with alert boxes you might want to disable them programmatically or create a separate mode in your code that would turn them off based on a condition (i.e. debug mode).
Macro recording programs don't work as well as you might think. Typically macro-recording programs record locations of mouse clicks and timing of key presses. The better macro-recording programs use (typically image processing) and (rarely) computer vision techniques. The solutions that use these techniques are typically VERY EXPENSIVE. (5k-10k per seat) [I can't remember one of the names of the better versions Might be QuickTest, but it was made by a company that was bought out by HP]
I was trying to interact with a webpage with autoit and the stupid javascript alerts kept breaking everything and I finally figured out how to disable them so I thought I would post it here:
#include <ie.au3>
$oIE = _IEAttach('https://www.site.com','URL')
$EventObject=ObjEvent($oIE.document,"IEEvent_")
Func IEEvent_onreadystatechange()
$readystate=$oIE.document.readystate
ConsoleWrite ($readystate & @CRLF )
if $readystate='interactive' then killalert()
EndFunc
while 1
sleep(100)
WEnd
func killalert()
Local $o_head = $oIE.document.all.tags("HEAD").Item(0)
Local $o_script = $oIE.document.createElement("script")
With $o_script
.language = "jscript"
.type = "text/javascript"
.text = 'function alert(message) {};'
EndWith
$o_head.appendChild($o_script)
EndFunc
Basically what this does is have a function get called in autoit when the page ready status='interactive' (apparently this is after most of the page has loaded but before it "runs" anything i think) that inserts some javascript into the page to redefine the alert() function so that it does nothing (no alert dialog to worry about having to click ok on). I have tested this and it works.
If you have the alert() function being called from a frame inside the page then you will have to use the onreadystatechange event and the readystate property of the frame instead of the main document.