Dgameman1

Why isn't Autoit able to click a Javascript Dialog?

50 posts in this topic

#41 ·  Posted (edited)

10 hours ago, junkew said:

iuiautomation thread shows can click allmost on everything thats recognized without blocking your script. See the examples.zip in that thread and a few of them deal with browsers. 

So, I figured I'd have to click the $Trashcan with iuiautomation and not click the OK on the javascript dialog box because the javascript dialog seems to freeze my script when clicked with _ieaction. I just don't know how to click the $Trashcan with iuiautomation

 

The code I attempted is below
 

;MsgBox("", "", StringTrimLeft($SubjectProfile.innertext, 3))
                            $TrashCan = $SubjectProfile.parentElement.nextElementSibling.nextElementSibling.nextElementSibling.firstElementChild.firstElementChild
                            ;Run(@WorkingDir & "/JavascriptOK.exe")
                            _IEAction($TrashCan, "click")


                            Local $oP1 = _UIA_getObjectByFindAll($UIA_oDesktop, "Title:=Messages - Internet Explorer;controltype:=UIA_PaneControlTypeId;class:=Alternate Modal Top Most", $treescope_children)
                            _UIA_Action($oP1, "setfocus")
                            Local $oP0 = _UIA_getObjectByFindAll($oP1, "Title:=Message from webpage;controltype:=UIA_WindowControlTypeId;class:=#32770", $treescope_children)
                            _UIA_Action($oP0, "setfocus")
                            _UIA_setVar("OK.mainwindow", "title:=OK;classname:=Button")
                            _UIA_action("OK.mainwindow", "setfocus")
                            _UIA_action("OK.mainwindow", "leftclick")

 

Edited by Dgameman1

Share this post


Link to post
Share on other sites



you have to study the examples from 1-30 in the iuiautomation thread. especially example 8

With complex hierarchy the _UIA_Action still has tricky defects in combination with _UIA_setVar not completely resolved.

What is the output of the simplespy?

maybe fallback to example 2 to use invokepattern

;~ Get the invoke pattern to click on the item
$oStart.getCurrentPattern($UIA_InvokePatternId, $pInvoke)
$oInvokeP=objCreateInterface($pInvoke, $sIID_IUIAutomationInvokePattern, $dtagIUIAutomationInvokePattern)
$oInvokeP.invoke()

 

Share this post


Link to post
Share on other sites

#43 ·  Posted (edited)

#include "UIAWrappers.au3"
AutoItSetOption("MustDeclareVars", 1)

Local $oP7=_UIA_getObjectByFindAll($UIA_oDesktop, "Title:=Messages  - Internet Explorer;controltype:=UIA_WindowControlTypeId;class:=IEFrame", $treescope_children)    
_UIA_Action($oP7,"setfocus")
Local $oP6=_UIA_getObjectByFindAll($oP7, "Title:=;controltype:=UIA_PaneControlTypeId;class:=Frame Tab", $treescope_children)    
_UIA_Action($oP6,"setfocus")
Local $oP5=_UIA_getObjectByFindAll($oP6, "Title:=Messages  - Internet Explorer;controltype:=UIA_PaneControlTypeId;class:=TabWindowClass", $treescope_children)    
_UIA_Action($oP5,"setfocus")
Local $oP4=_UIA_getObjectByFindAll($oP5, "Title:=;controltype:=UIA_PaneControlTypeId;class:=Shell DocObject View", $treescope_children)    
_UIA_Action($oP4,"setfocus")
Local $oP3=_UIA_getObjectByFindAll($oP4, "Title:=http://www.WEBSITE.com/messages;controltype:=UIA_PaneControlTypeId;class:=Internet Explorer_Server", $treescope_children)    
_UIA_Action($oP3,"setfocus")
Local $oP2=_UIA_getObjectByFindAll($oP3, "Title:=Messages;controltype:=UIA_PaneControlTypeId;class:=", $treescope_children)    
Local $oP1=_UIA_getObjectByFindAll($oP2, "Title:=;controltype:=UIA_ListControlTypeId;class:=", $treescope_children)    
Local $oP0=_UIA_getObjectByFindAll($oP1, "Title:=To Dgameman1

 

Edited by Dgameman1

Share this post


Link to post
Share on other sites

#44 ·  Posted (edited)

I make a mistake/error in my example (post #31)

There should be:

Func CloseAlert()
    Sleep(3000)
    ControlClick("[CLASS:#32770]", "Someone clicked the document!", "[CLASS:Button; INSTANCE:1]")
EndFunc   ;==>CloseAlert

But even that fix is not able to properly close the window.

but ......

Yesterday @Chimp @Jos an me discused here:

 

 

and here is working solution for HOW TO CLICK IN JAVA ALLERT WINDOW :)

;~ https://www.autoitscript.com/forum/topic/181278-why-isnt-autoit-able-to-click-a-javascript-dialog/
;~ https://www.autoitscript.com/forum/topic/181491-question-about-autoit3executeline/
#include <IE.au3>
#include <MsgBoxConstants.au3>

_Example()

Func _Example()
    ; Open a browser with the basic example page,
    Local $oIE = _IE_Example("basic")
    ; Insert an event script into the head of the document that creates a JavaScript alert when someone clicks on the document
    _IEHeadInsertEventScript($oIE, "document", "onclick", "alert('Someone clicked the document!');")
    ; Run second AutoIt instance to click in the backround
    _ExecuteInBackground()
    ; Click first link on the page
    $oIE.document.links.item(0).click()
    ; Testing
    MsgBox($MB_ICONINFORMATION, 'Test', 'Was clicked')
    _IEQuit($oIE)
EndFunc   ;==>_Example

Func _ExecuteInBackground()
    Local $sCommand = _
            'Local ' & _
            '$Dummy1 = WinWaitActive("[CLASS:#32770]", "Someone clicked the document!"), ' & _
            '$Dummy2 = Sleep(1000), ' & _
            '$Dummy3 = ControlClick("[CLASS:#32770]", "Someone clicked the document!", "[CLASS:Button; INSTANCE:1]") ' & _
            ''

    $sCommand = '"' & StringReplace($sCommand, '"', '""') & '"'
    ConsoleWrite($sCommand & @CRLF)
    Run(@AutoItExe & ' /AutoIt3ExecuteLine ' & $sCommand)
EndFunc   ;==>_ExecuteInBackground

 

Edited by mLipok
example script CleanUp
1 person likes this

Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest begining - comunication with GitHub REST API *
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST API *

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * 

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2017-06-04

Share this post


Link to post
Share on other sites
51 minutes ago, mLipok said:

Yesterday @Chimp @Jos an me discused here:

 

 

and here is working solution for HOW TO CLICK IN JAVA ALLERT WINDOW :)

;~ https://www.autoitscript.com/forum/topic/181278-why-isnt-autoit-able-to-click-a-javascript-dialog/
;~ https://www.autoitscript.com/forum/topic/181491-question-about-autoit3executeline/
#include <IE.au3>
#include <MsgBoxConstants.au3>

_Example()

Func _Example()
    ; Open a browser with the basic example page,
    Local $oIE = _IE_Example("basic")
    ; Insert an event script into the head of the document that creates a JavaScript alert when someone clicks on the document
    _IEHeadInsertEventScript($oIE, "document", "onclick", "alert('Someone clicked the document!');")
    ; Run second AutoIt instance to click in the backround
    _ExecuteInBackground()
    ; Click first link on the page
    $oIE.document.links.item(0).click()
    ; Testing
    MsgBox($MB_ICONINFORMATION, 'Test', 'Was clicked')
    _IEQuit($oIE)
EndFunc   ;==>_Example

Func _ExecuteInBackground()
    Local $sCommand = _
            'Local ' & _
            '$Dummy1 = WinWaitActive("[CLASS:#32770]", "Someone clicked the document!"), ' & _
            '$Dummy2 = Sleep(1000), ' & _
            '$Dummy3 = ControlClick("[CLASS:#32770]", "Someone clicked the document!", "[CLASS:Button; INSTANCE:1]") ' & _
            ''

    $sCommand = '"' & StringReplace($sCommand, '"', '""') & '"'
    ConsoleWrite($sCommand & @CRLF)
    Run(@AutoItExe & ' /AutoIt3ExecuteLine ' & $sCommand)
EndFunc   ;==>_ExecuteInBackground

 

 

I tested it and it still didn't work!!

 

Here is what I have so far

 

$WEBSite = _IECreate("WEBSITE.COM")

;$iPid = Run(@WorkingDir & "/JavascriptOK.exe")
$CheckInbox = 0
$InboxURL = "http://www.WEBSITE.com/messages"
Do
    _IENavigate($WEBSite, $InboxURL)
    $im_footer = _IEGetObjById($WEBSite, "footer_nav")
    Do
        $OriginalPosition = $WEBSite.document.body.scrollHeight
        If $DeleteDeletedProfiles Then
            $ProfileImages = _IETagNameGetCollection($WEBSite, "img")
            For $DeletedProfileImage In $ProfileImages
                If $DeletedProfileImage.src = "http://WEBCSITE.COM/pq_60.png" And StringInStr($DeletedProfileImage.alt, "An image of") Then
                    $TrashCan = $DeletedProfileImage.parentElement.parentElement.nextElementSibling.nextElementSibling.nextElementSibling.firstElementChild.firstElementChild
                    _IEAction($TrashCan, "click")
                    _ExecuteInBackground()
                EndIf
            Next
        EndIf
        _IEAction($im_footer, "scrollintoview")
        $CheckScrollChange = TimerInit()
        $NewInboxes = 0
        While (TimerDiff($CheckScrollChange) <= 3000)
            If $OriginalPosition <> $WEBSite.document.body.scrollHeight Then

                $NewInboxes = 1
                ExitLoop
            EndIf
        WEnd
    Until $NewInboxes = 0
    $CheckInbox += 1
    If $CheckInbox = 2 Then
        $InboxURL = "http://www.WEBSITE.com/messages?folder=2"
    EndIf
Until $CheckInbox >= $HowManyTimes + 2
;ProcessClose($iPid)


Func _ExecuteInBackground()
    Local $sCommand = _
            'Local ' & _
            '$Dummy1 = WinWaitActive("[CLASS:#32770]", ""), ' & _
            '$Dummy2 = Sleep(1000), ' & _
            '$Dummy3 = ControlClick("[CLASS:#32770]", "", "[CLASS:Button; INSTANCE:1]") ' & _
            ''

    $sCommand = '"' & StringReplace($sCommand, '"', '""') & '"'
    ConsoleWrite($sCommand & @CRLF)
    Run(@AutoItExe & ' /AutoIt3ExecuteLine ' & $sCommand)
EndFunc   ;==>_ExecuteInBackground

Still Doesn't work =/

 

Quote

>>>> Window <<<<
Title:    Message from webpage
Class:    #32770
Position:    779, 465
Size:    380, 172
Style:    0x94C801C5
ExStyle:    0x00010101
Handle:    0x000E08DC

>>>> Control <<<<
Class:    Button
Instance:    1
ClassnameNN:    Button1
Name:    
Advanced (Class):    [CLASS:Button; INSTANCE:1]
ID:    1
Text:    OK
Position:    172, 96
Size:    88, 26
ControlClick Coords:    39, 15
Style:    0x50030001
ExStyle:    0x00000004
Handle:    0x00180916

>>>> Mouse <<<<
Position:    998, 607
Cursor ID:    0
Color:    0xA0E1E1

>>>> StatusBar <<<<

>>>> ToolsBar <<<<

>>>> Visible Text <<<<
OK
Cancel
Deleting these messages will also delete their
responses. Are you sure you want to delete them?


>>>> Hidden Text <<<<

 

Share this post


Link to post
Share on other sites

Very very weird. I tested it on another website and it's working fine....

 

#include <ie.au3>
#include <array.au3>
$WEBSite = _IECreate("http://www.sislands.com/coin70/week1/dialogbox.htm")

;$iPid = Run(@WorkingDir & "/JavascriptOK.exe")

    $ProfileImages = _IETagNameGetCollection($WEBSite, "input")
    For $DeletedProfileImage In $ProfileImages
        ;ConsoleWrite($DeletedProfileImage.value)
        If $DeletedProfileImage.value = "confirm" Then
            _IEAction($DeletedProfileImage, "click")
            _ExecuteInBackground()
        EndIf
    Next

;ProcessClose($iPid)


Func _ExecuteInBackground()
    Local $sCommand = _
            'Local ' & _
            '$Dummy1 = WinWaitActive("[CLASS:#32770]", ""), ' & _
            '$Dummy2 = Sleep(1000), ' & _
            '$Dummy3 = ControlClick("[CLASS:#32770]", "", "[CLASS:Button; INSTANCE:1]") ' & _
            ''

    $sCommand = '"' & StringReplace($sCommand, '"', '""') & '"'
    ConsoleWrite($sCommand & @CRLF)
    Run(@AutoItExe & ' /AutoIt3ExecuteLine ' & $sCommand)
EndFunc   ;==>_ExecuteInBackground

 

Share this post


Link to post
Share on other sites

#47 ·  Posted (edited)

OMG I typoed.

 

IT WORKS!!

THANK YOU SO MUCH HOLY HELL

 

EDIT: It only works if I first click cancel or ok manually, and then the bot starts working.

Edited by Dgameman1

Share this post


Link to post
Share on other sites

:)

 


Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest begining - comunication with GitHub REST API *
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST API *

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * 

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2017-06-04

Share this post


Link to post
Share on other sites

Calling a method click

_IEAction($DeletedProfileImage, "click")

is something different then doing a mouseclick that implicitly triggers later one or multiple methods

; Double click at the x, y position of 0, 500.
MouseClick($MOUSE_CLICK_LEFT, 0, 500, 2)

Calculating your element location and using mouseclick saves you the trouble of 2nd script

Share this post


Link to post
Share on other sites

#50 ·  Posted (edited)

6 hours ago, Dgameman1 said:

...<zot>...

EDIT: It only works if I first click cancel or ok manually, and then the bot starts working.

Hi

I think that the external call (external click) should be done on the "$DeletedProfileImage" object so that (I believe) subseguent code should run fluidly.
In short, could You please try this:

  • 1) Insert the following 3 service functions at bottom of your main script (those are general purpose and not specific to this case):
; pass the $oIE reference to the browser and the $oNode reference to the element
Func _GetReferences(ByRef $oIE, ByRef $oElement)
    Local $hoIE = HWnd($oIE.HWnd()) ; get the handle of the browser
    Local $ObjShell = ObjCreate("Shell.Application")
    Local $ObjShellWindows = $ObjShell.Windows(); collection of all ShellWindows (IE and File Explorer)
    Local $aRefs[3] ; [0] HWND of browser; [1] item nr within Shell.Application.Windows; [0] xPath of Element
    Local $ObjTempIE ; temp variable
    For $item = 0 To ($ObjShellWindows.count) - 1
        ;
        $ObjTempIE = $ObjShellWindows.item($item) ; scann all Windows in the collection
        If HWnd($ObjTempIE.HWnd()) = $hoIE Then ; Found reference to hour browser
            $aRefs[0] = $hoIE ; HWND
            $aRefs[1] = $item ; item
            $aRefs[2] = _GetPathTo($oElement) ; Path
            $ObjTempIE = ""
            Return SetError(0, 0, $aRefs)
        EndIf
        $ObjTempIE = ""
    Next
    Return SetError(1, 0, -1)
EndFunc   ;==>_GetReferences

; get xPath of an element
; http://stackoverflow.com/questions/2631820/im-storing-click-coordinates-in-my-db-and-then-reloading-them-later-and-showing/2631931#2631931
; answer by Scott Izu
Func _GetPathTo($oElement)
    If $oElement.tagName = 'HTML' Then Return '/HTML[0]'
    If $oElement = $oElement.document.body Then Return '/HTML[0]/BODY[0]'
    Local $ix = 0, $sibling
    Local $siblings = $oElement.parentNode.childNodes;
    For $sibling In $siblings
        If ($sibling = $oElement) Then
            Return _GetPathTo($oElement.parentNode) & '/' & $oElement.tagName & '[' & ($ix) & ']'
        EndIf
        If ($sibling.nodeType = 1 And $sibling.tagName = $oElement.tagName) Then $ix += 1
    Next
EndFunc   ;==>_GetPathTo

Func _CallFromExternal(ByRef $aRefs)
    ; replacing the $aRefs[*] with it's values whe should has
    Local $sMethod = StringReplace(StringReplace(StringReplace($aRefs[2], '/', '.getElementsByTagname("'), '[', '").item('), ']', ')') & ".click()"
    Local $sCommandLine = 'local $ObjShell = ObjCreate("Shell.Application"), $ObjShellWindows = $ObjShell.Windows(), $ObjIE = $ObjShellWindows.item(' & $aRefs[1] & '), $oDocument = $ObjIE.Document, $Dummy = $oDocument' & $sMethod ; Execute("$oDocument"' & $sMethod & ')'
    $sCommandLine = StringReplace($sCommandLine, '"', '""')
    $sCommandLine = '"' & $sCommandLine & '"'
    ; and now we can run it from 'external'
    ; MsgBox(0, "Debug", $sCommandLine)
    Run(@AutoItExe & ' /AutoIt3ExecuteLine ' & $sCommandLine)
    ; MsgBox(0, "Debug", $sCommandLine)
EndFunc   ;==>_CallFromExternal

 

  • 2) Instead of using  _IEAction($DeletedProfileImage, "click"), you should do like this:
  • 2.1) Call the  _GetReferences() passing the current browser obj $WEBSite and the target obj to be clicked $DeletedProfileImage
    Local $aArguments = _GetReferences($WEBSite, $DeletedProfileImage)

     

  • 2.2) Call the _CallFromExternal() function passing the array of arguments returned from the previous function. Those arguments allow the external function to connect to the objects we are working on in the main script so to perform the "click" on it "from the outside of the main script".
    _CallFromExternal($aArguments)

     

  • 3) After this you should be able to manage the popup message with one of the regular ways as you like

if you want to try, please let us know what happens....

Edited by Chimp
variable names editing

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


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