Jump to content
Robdog1955

Cannot click button in IE

Recommended Posts

Robdog1955

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

 

Share this post


Link to post
Share on other sites
Danp2

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.

Share this post


Link to post
Share on other sites
Robdog1955

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!

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

  • Similar Content

    • Juvigy
      By Juvigy
       Hi Guys,
      Could you please try out my script on win10 machine (simple site scrape)? One of my users complains it doesnt work on his win10, but it works fine on my win7. The error he gets is on the .FireEvent call. I think it is admin rights or IE / Edge issue, but don't have win10 to test it out.
      Thanks in advance.
       
      #include <IE.au3> #include <Array.au3> #include <Excel.au3> Global $oIE,$string Dim $destination[8] = ["Viña del Mar", "Rancagua", "Pucon", "Copiapo", "Temuco", "La serena", "Puerto Montt", "Valdivia"] Dim $FinalResult[1][3] $FinalResult[0][0] = "Destination" $FinalResult[0][1] = "Ida" $FinalResult[0][2] = "Vuelta" Attach("https://www.turbus.cl/") If IsObj($oIE) = 0 Then ConsoleWrite("IE error??"&@CRLF) $oIE = _IECreate() EndIf For $i=0 to UBound($destination)-1 Step 1 _IENavigate($oIE,"https://www.turbus.cl/") $site1 = _IEGetObjById($oIE,"j_id_id122:cmbCiudadOrigenV2") While @error Sleep(1000) $site1 = _IEGetObjById($oIE,"j_id_id122:cmbCiudadOrigenV2") WEnd $site2 = _IEGetObjById($oIE,"j_id_id122:cmbCiudadDestinoV2") $date1 = _IEGetObjById($oIE,"j_id_id122:calIdaV2InputDate") $date2 = _IEGetObjById($oIE,"j_id_id122:calVueltaV2InputDate") $buttun1 = _IEGetObjById($oIE,"j_id_id122:botonContinuarV2") $site1.Value = "Santiago" $site2.Value = $destination[$i] $date1.Value = @MDAY+1&"/"&@MON&"/"&@YEAR $date2.Value = @MDAY+4&"/"&@MON&"/"&@YEAR _IEAction($buttun1,"click") _IELoadWait($oIE) $array = GetResult(GetData()) If IsArray($array) = 0 OR UBound($array,1) < 1 Or UBound($array,2) < 2 Then MsgBox(0,UBound($array,1), UBound($array,2)) _ArrayDisplay($array,"$array") ContinueLoop EndIf _ArrayAdd($FinalResult,"Santiago-"&$destination[$i]&"|"&$array[0][0]&"|"&$array[0][1]) Next ;~ _ArrayDisplay($FinalResult) Local $oExcel = _Excel_Open() If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeWrite Example", "Error creating the Excel application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended) Local $oWorkbook = _Excel_BookNew($oExcel) If @error Then MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeWrite Example", "Error creating the new workbook." & @CRLF & "@error = " & @error & ", @extended = " & @extended) _Excel_Close($oExcel) Exit EndIf _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $FinalResult, "A1") If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeWrite Example 2", "Error writing to worksheet." & @CRLF & "@error = " & @error & ", @extended = " & @extended) Func GetData() Local $string1,$string2 $ida = _IEGetObjById($oIE,"tbIda_lbl") While @error Sleep(1000) $ida = _IEGetObjById($oIE,"tbIda_lbl") WEnd _IEAction($ida,"click") Sleep(2000) $result1 = _IEGetObjById($oIE,"pnlReglaIda:idPersonalizaPasaje:tb") While @error $ida.FireEvent("onmouseover") Sleep(1000) $ida.FireEvent("onclick") Sleep(1000) $result1 = _IEGetObjById($oIE,"pnlReglaIda:idPersonalizaPasaje:tb") WEnd $string1 = $string1 & $result1.innertext&@CRLF $vuelta = _IEGetObjById($oIE,"tbVuelta_lbl") _IEAction($vuelta,"click") Sleep(2000) $result2 = _IEGetObjById($oIE,"pnlReglaVuelta:idPersonalizaPasajeRegreso:tb") While @error $vuelta.FireEvent("onmouseover") Sleep(1000) $vuelta.FireEvent("onclick") Sleep(1000) $result2 = _IEGetObjById($oIE,"pnlReglaVuelta:idPersonalizaPasajeRegreso:tb") WEnd $string2 = $string2 & $result2.innertext&@CRLF Return $string1&"|"&$string2 EndFunc Func Attach($atachadres) Local $i = 1 While 1 $oIE = _IEAttach("", "instance", $i) If @error = $_IEStatus_NoMatch Then $oIE = 0 ExitLoop EndIf If StringLeft(_IEPropertyGet($oIE, "locationurl"),StringLen($atachadres)) = $atachadres Then ExitLoop $i += 1 WEnd EndFunc Func GetResult($String) Dim $Master[0][2] ,$Master2[1][2] $2strings = StringSplit($String,"|",2) $Strings = StringSplit($2strings[0],@CRLF,2) $Strings = _ArrayUnique($Strings) For $i=0 to UBound($Strings)-1 Step 1 $Data = StringSplit($Strings[$i],"$",2) $add = _ArrayToString($Data) _ArrayAdd($Master,$add) Next For $i=UBound($Master,1)-1 to 0 Step -1 If $Master[$i][1] = "" Then _ArrayDelete($Master,$i) Next _ArraySort($Master,0, 0, 0,1) $Master2[0][0] = $Master[0][0]&$Master[0][1] Dim $Master[0][2] $Strings = StringSplit($2strings[1],@CRLF,2) $Strings = _ArrayUnique($Strings) For $i=0 to UBound($Strings)-1 Step 1 $Data = StringSplit($Strings[$i],"$",2) $add = _ArrayToString($Data) _ArrayAdd($Master,$add) Next For $i=UBound($Master,1)-1 to 0 Step -1 If $Master[$i][1] = "" Then _ArrayDelete($Master,$i) Next _ArraySort($Master,0, 0, 0,1) $Master2[0][1] = $Master[0][0]&$Master[0][1] Return $Master2 EndFunc  
    • SkysLastChance
      By SkysLastChance
      I have a goofy problem. I am hoping someone could shed some light. The example is not going around the text box. It is way off. 
      I have seen some post blaming IE 11, however I have IE11 on my desktop and it works fine.
      Is there anything I can do that might fix this? 
       
      ; Open a browser with the form example and get a reference to the form ; textarea element. Get the coordinates and dimensions of the text area, ; outline its shape with the mouse and come to rest in the center #include <IE.au3> Local $oIE = _IE_Example("form") Local $oForm = _IEFormGetObjByName($oIE, "ExampleForm") Local $oTextArea = _IEFormElementGetObjByName($oForm, "textareaExample") ; Get coordinates and dimensions of the textarea Local $iScreenX = _IEPropertyGet($oTextArea, "screenx") Local $iScreenY = _IEPropertyGet($oTextArea, "screeny") Local $iWidth = _IEPropertyGet($oTextArea, "width") Local $iHeight = _IEPropertyGet($oTextArea, "height") ; Outline the textarea with the mouse, come to rest in the center Local $iMousespeed = 50 MouseMove($iScreenX, $iScreenY, $iMousespeed) MouseMove($iScreenX + $iWidth, $iScreenY, $iMousespeed) MouseMove($iScreenX + $iWidth, $iScreenY + $iHeight, $iMousespeed) MouseMove($iScreenX, $iScreenY + $iHeight, $iMousespeed) MouseMove($iScreenX, $iScreenY, $iMousespeed) MouseMove($iScreenX + $iWidth / 2, $iScreenY + $iHeight / 2, $iMousespeed)  
       
    • FMS
      By FMS
      Hello,
      I try to get all the text from a news site around a subject.
      The first run I get all the text inside a attribute in an array.
      When i try to go back and reload another page it chrashes and think it because "$oIE.GoBack"
      I couldn't find anything in the help/forum around this subject.
      Do I need to reload the $oIE or something afther an X.GoBack?
      The error i got is :
      if $oTag2.GetAttribute("class") == "NewsDetail" Then if $oTag2^ ERROR I'm not shure why I got this error, maybe someone could explain?
      Also I'm open for some pointers in this test script because I'm pretty new in working whit the IE.UDF
      Maybe there is an simpler way to get the same results?

      test script:
      #include <IE.au3> #include <MsgBoxConstants.au3> #include <Array.au3> HotKeySet("{ESC}", "Terminate") Global $oIE = _IECreate("https://www.iex.nl/Zoeken/Nieuws.aspx?q=air%20france") ;get first subject Global $oLink1 = _IEGetObjById($oIE, "ctl00_ctl00_Content_LeftContent_NewsSearch_repNews_ctl00_linkNews") Sleep(500) _IEAction($oLink1, "click") Sleep(500) Global $oTags = _IETagNameGetCollection($oIE, "div") Global $aResults[1] For $oTag In $oTags if $oTag.GetAttribute("class") == "NewsDetail" Then _ArrayAdd($aResults, $oTag.innerTEXT) EndIf Next $aResults[0] = UBound($aResults) - 1 _ArrayDisplay($aResults, "Episodelist") ConsoleWrite($aResults[1] & @CRLF) $oIE.GoBack ;get second subject Global $oLink2 = _IEGetObjById($oIE, "ctl00_ctl00_Content_LeftContent_NewsSearch_repNews_ctl01_linkNews") Sleep(500) _IEAction($oLink2, "click") Sleep(500) Local $oTags2 = _IETagNameGetCollection($oIE, "div") Local $aResults2[1] For $oTag2 In $oTags2 if $oTag2.GetAttribute("class") == "NewsDetail" Then _ArrayAdd($aResults2, $oTag2.innerTEXT) EndIf Next $aResults2[0] = UBound($aResults2) - 1 ConsoleWrite($aResults2[1] & @CRLF) Func Terminate() _IEQuit($oIE) Exit EndFunc ;==>Terminate  
    • kelso
      By kelso
      Hello Guru's,
       
      I'm trying to write an autoit script to select from the dropdown list as you see in the attached picture. 
      I read the help page for _IEFormElementOptionSelect, but I cannot grasp how to correlate that with the source code that I'm seeing. any suggestions?

    • SkysLastChance
      By SkysLastChance
      I am having a hard time understanding why this is not working. I was hoping some one could help explain it to me. 
      $tags = $oIE.document.GetElementsByTagName("input") For $tag in $tags $class_value = $tag.GetAttribute("class") If string($class_value) = "fTs-p3298-l0 wplEditControl" Then $target = $tag ExitLoop EndIF Next MsgBox(0,"",$target) If $target = "fTs-p3298-l0 wplEditControl" THEN MsgBox(0,"","itworked") I have tried 
       MsgBox(0,"",$target.Attribute)  MsgBox(0,"",$target.Value)  MsgBox(0,"",$target.InnerText) I would expect to see this in the msgbox
      fTs-p3298-l0 wplEditControl  
×