Jump to content

Cannot click button in IE


Recommended Posts

I'm trying to click a button on a web page. I have added a couple of MsgBox lines to allow me to watch what happens on the page. As you can see the first half of my script enters data into text boxes on the page. I have no problem there. I just cannot click on the region buttons. The "set focus" line causes an outline to appear around the EU button and the "click button" line causes the "Pick a Region" text to disappear. Here is the code I have so far.

#include <IE.au3>

Local $oIE = _IECreate("http://questchecker.com/")

Local $iQuestID = "123456"
Local $sCharacterName = "CharacterName"
Local $colForms = _IEFormGetCollection($oIE)
$iCount = 0
For $oForm In $colForms
    $oFormElements = _IEFormElementGetCollection($oForm)
    For $oFormElement In $oFormElements
        $iCount = $iCount + 1
        Local $sTagName = StringLower($oFormElement.tagName)
        Local $sElementType = $oFormElement.type
        Local $sElementName = $oFormElement.name
        Switch $iCount
            Case 6
                _IEFormElementSetValue($oFormElement, "MyRealm", 0) ; realm
            Case 7
                _IEFormElementSetValue($oFormElement, $sCharacterName, 0)
            Case 8
                _IEFormElementSetValue($oFormElement, $iQuestID, 0)
        EndSwitch
    Next
Next

Local $oButtons = _IEGetObjByName($oIE, "questForm")
For $oButton In $oButtons
    If _IEFormElementGetValue($oButton) = "US" Then
        MsgBox(0, "", "Click Okay to set focus")
        _IEAction($oButton, "focus")
        MsgBox(0, "", "Click Okay to click button")
        _IEAction($oButton, "click")
        ExitLoop
    EndIf
Next
MsgBox(0, "", "Click Okay to quit")
_IEQuit($oIE)
Exit

 

Link to comment
Share on other sites

First, not sure why you are performing the nested loops. You can simply use the regular _IE commands to retrieve the form and it's elements. That said, this is a tricky one because of the use of jQuery and an older version of YUI.

Here's the solution that I came up with --

Local $oIE = _IECreate("http://questchecker.com/")


Local $iQuestID = "123456"
Local $sCharacterName = "test"
Local $sRealm = "Area 52"

Local $oForm = _IEFormGetObjByName ($oIE, "questForm")

Local $jQuery = _jQuerify($oIE)
$jQuery(".us").click()
$jQuery(".us").addClass("yui-button-checked yui-radio-button-checked")

Local $oElement = _IEFormElementGetObjByName($oForm, "realm")
_IEFormElementSetValue($oElement, $sRealm)
$oElement = _IEFormElementGetObjByName($oForm, "name")
_IEFormElementSetValue($oElement, $sCharacterName)
$oElement = _IEFormElementGetObjByName($oForm, "check")
_IEFormElementSetValue($oElement, $iQuestID)

$jQuery(".checkButton").trigger('click')


; #FUNCTION# ====================================================================================================================
; Name ..........: _jQuerify
; Description ...:
; Syntax ........: _jQuerify(Byref $oIE)
; Parameters ....: $oIE                 - Object variable of an InternetExplorer.Application.
; Return values .: an object variable pointing to the jQuery library
; Author ........: Chimp
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......:
; ===============================================================================================================================
Func _jQuerify(ByRef $oIE)

    Local $msie, $jsEval, $jQuery, $otherlib = False

    $msie = Execute('$oIE.document.documentMode')

    If ($msie = "") Or Number($msie) < 11 Then ; an IE version < 11
        ; create a reference to the javascript eval() function
        $oIE.document.parentWindow.setTimeout('window.eval = eval', 0)
        Do
            Sleep(250)
            $jsEval = Execute('$oIE.Document.parentwindow.eval')
        Until IsObj($jsEval)

    Else ; IE version > = 11
        ; create a reference to the javascript eval() function
        $oIE.document.parentWindow.setTimeout('document.head.eval = eval', 0)
        Do
            Sleep(250)
            $jsEval = Execute('$oIE.Document.head.eval')
        Until IsObj($jsEval)

    EndIf

    ; if jQuery is not already loaded then load it
    If $jsEval("typeof jQuery=='undefined'") Then

        ; check if the '$' (dollar) name is already in use by other library
        If $jsEval("typeof $=='function'") Then $otherlib = True

        Local $oScript = $oIE.document.createElement('script');
        $oScript.type = 'text/javascript'

        ; If you want to load jQuery from a disk file use the following statement
        ; where i.e. jquery-1.9.1.js is the file containing the jQuery source
        ; (or also use a string variable containing the whole jQuery listing)
;~ $oScript.TextContent = FileRead(@ScriptDir & "\jquery-1.9.1.js") ; <--- from a file

        ; If you want to download jQuery from the web use this statement
        $oScript.src = 'https://code.jquery.com/jquery-latest.min.js' ; <--- from an url


        $oIE.document.getElementsByTagName('head').item(0).appendChild($oScript)
        Do
            Sleep(250)
        Until $jsEval("typeof jQuery == 'function'")
    EndIf

    Do
        Sleep(250)
        $jQuery = $jsEval("jQuery")
    Until IsObj($jQuery)

    If $otherlib Then $jsEval('jQuery.noConflict();')

    Return $jQuery
EndFunc   ;==>_jQuerify

There may be a better way, so I welcome others input / feedback.

Link to comment
Share on other sites

Thank you so very very much. I have spent a lot of time on this (days). I searched a lot and tried a lot.

My problem is that I do not have a good understanding. I am trying to learn.

I hope you do not feel that you simply did it for me because I will be studying what you did and learn from it!

Link to comment
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
 Share

×
×
  • Create New...