Jump to content
aloalo

Help with loop returning an element

Recommended Posts

aloalo

Hello again. I have having some trouble passing an object out of a loop properly in order to deal with it more nicely outside of the function searching for it. The thing I want passed out is the $oTag object, but I can't seem to do anything with the object after my function returns.

Func FrameCrawlerTagSearch($poFrame, $pTag, $pProperty, $pPropertyValue, $pAction = "click", $pLoadWait = 1, $pReturn = 0)
    Local $bDone = False
    _IELoadWait($poFrame)
    $Test = _IEDocReadHTML($poFrame)
    $oTags = _IETagNameAllGetCollection($poFrame)
    For $oTag In $oTags
        If $oTag.tagname = $pTag Then
            If $pProperty = "id" And $oTag.id = $pPropertyValue Then
                MsgBox(0, "", $oTag.id & $pReturn)
                _IEAction($oTag, $pAction)
                If $pLoadWait = 1 Then
                    _IELoadWait($poFrame)
                EndIf
                If $pReturn = 1 Then
                    MsgBox(0, "", "Returning the tag " & $oTag.tagname)
                    Return $oTag
                EndIf
            ElseIf $pProperty = "name" And $oTag.name = $pPropertyValue Then
                _IEAction($oTag, $pAction)
                If $pLoadWait = 1 Then
                    _IELoadWait($poFrame)
                EndIf
            ElseIf $pProperty = "innertext" And StringInStr($oTag.innertext, $pPropertyValue) Then
;~              MsgBox(0, "", $oTag.innertext)
                _IEAction($oTag, $pAction)
                If $pLoadWait = 1 Then
                    _IELoadWait($poFrame)
                EndIf
            EndIf
            $dxCounter = $dxCounter + 1
        EndIf
    Next
    $aSubFrames = _IEFrameGetCollection($poFrame)
    $iNumFrames = @extended
    If $iNumFrames > 0 Then
        $i = 0
        While Not $bDone And $i < $iNumFrames
            $oSubFrame = _IEFrameGetCollection($aSubFrames, $i)
            $bDone = FrameCrawlerTagSearch($oSubFrame, $pTag, $pProperty, $pPropertyValue, $pAction, $pLoadWait, $pReturn)
            If $bDone Then
                Return $bDone
            EndIf
            $i = $i + 1
        WEnd
    EndIf
    Return False
EndFunc   ;==>FrameCrawlerTagSearch

 

Share this post


Link to post
Share on other sites
jdelaney

You will probably need to return an array of objects: 1 for the specific frame object you found it in, and another for the $oTag.


IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites
mLipok

I suppose the problem is here:

If $bDone Then
                Return $bDone
            EndIf

here is proposal to fix:

Func FrameCrawlerTagSearch($poFrame, $pTag, $pProperty, $pPropertyValue, $pAction = "click", $pLoadWait = 1, $pReturn = 0)
    Local $bDone = False
    _IELoadWait($poFrame)
    $Test = _IEDocReadHTML($poFrame)
    $oTags = _IETagNameAllGetCollection($poFrame)
    For $oTag In $oTags
        If $oTag.tagname = $pTag Then
            If $pProperty = "id" And $oTag.id = $pPropertyValue Then
                MsgBox(0, "", $oTag.id & $pReturn)
                _IEAction($oTag, $pAction)
                If $pLoadWait = 1 Then
                    _IELoadWait($poFrame)
                EndIf
                If $pReturn = 1 Then
                    MsgBox(0, "", "Returning the tag " & $oTag.tagname)
                    Return SetError(0, 1, $oTag)
                EndIf
            ElseIf $pProperty = "name" And $oTag.name = $pPropertyValue Then
                _IEAction($oTag, $pAction)
                If $pLoadWait = 1 Then
                    _IELoadWait($poFrame)
                EndIf
            ElseIf $pProperty = "innertext" And StringInStr($oTag.innertext, $pPropertyValue) Then
;~              MsgBox(0, "", $oTag.innertext)
                _IEAction($oTag, $pAction)
                If $pLoadWait = 1 Then
                    _IELoadWait($poFrame)
                EndIf
            EndIf
            $dxCounter = $dxCounter + 1
        EndIf
    Next
    $aSubFrames = _IEFrameGetCollection($poFrame)
    $iNumFrames = @extended
    If $iNumFrames > 0 Then
        $i = 0
        While Not $bDone And $i < $iNumFrames
            $oSubFrame = _IEFrameGetCollection($aSubFrames, $i)
            $bDone = FrameCrawlerTagSearch($oSubFrame, $pTag, $pProperty, $pPropertyValue, $pAction, $pLoadWait, $pReturn)
            If @extended = 1 Then
                Return SetError(0, 2, $bDone)
            EndIf
            $i = $i + 1
        WEnd
    EndIf
    Return SetError(1, 3, False)
EndFunc   ;==>FrameCrawlerTagSearch

 

EDIT:
btw. if you 

$pReturn = 0

then you always return in 

Return SetError(1, 3, False)
Edited by mLipok

Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest begining - comunication with GitHub REST API Forum Rules *
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 APIErrorLog.au3 UDF - A logging Library
 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 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 * Good coding practices in AutoIt * 

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) * IE in TaskScheduler

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: 2018-10-31

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

    • ur
      By ur
      Is there any UDF to remove all anchor tags <a> with a particular class (and also its sub elements completely) in a html document.
      Here the classes are browse and breadcrumbs
      Like in the below image.


       
      I am not able to find that option in IE.au3
       
      Please suggest.
    • milkmoron
      By milkmoron
      I am trying to automate something in a web browser but i need some help with finding the html code to a web applet. How do I access the code.
    • Seminko
      By Seminko
      Is there a way to grab non-hardcoded but rather javascript generated data from a webpage?
      Tried a get request as well as _IEBodyReadHTML but both seem to grab the code without the javascript generated data.
      $oHTTP = ObjCreate("winhttp.winhttprequest.5.1") $oHTTP.Open("GET", "link", False) $oHTTP.Send() $oReceived = $oHTTP.ResponseText $oStatusCode = $oHTTP.Status Global $DataArray[10][5] If $oStatusCode <> 200 Then Exit MsgBox(1, "Error", "Status Code <> 200") EndIf FileWrite(@ScriptDir & "\output.txt", $oReceived) ; //////// #include <IE.au3> Local $FullLink = "link" Local $oIE = _IECreate($FullLink, 0, 0) _IELoadWait($oIE) Local $sText = _IEBodyReadHTML($oIE) FileWrite(@ScriptDir & "\output.txt", $sText)  
    • SkysLastChance
      By SkysLastChance
       
      WinActivate("MEDITECH - Internet Explorer") Sleep (500) $oIE = _IEAttach("MEDITECH") $oDiv1 = _IEGetObjById($oIE, "sysmenu-searchbarbutton") _IEAction($oDiv1, "click") I am just trying to click the little magnifying glass, next to the gear button with no luck. I was hoping someone might have an idea why this is not working?
       

    • Burgs
      By Burgs
      Hello,
        I have a website with a Google Map I setup using the Google Map API.  It works and displays just fine.  However to make it useful to me I need to be able to dynamically change the map to display different areas by sending new Latitude and Longitude coordinates.  I am having difficulty making this happen.  Here is my code thus far:
      #include <IE.au3> $oIE3 = _IECreate("http://my_sample_website.html") ;just an example, not an actual site... _IELoadWait($oIE3) $s_word = "lat:" $oInputs = _IETagNameAllGetCollection($oIE3) if @error <> 0 Then MsgBox($MB_SYSTEMMODAL, "ERROR", "Error is: " & @error) EndIf ;@error For $oInput In $oInputs if Number($iPos) == -1 Then $iPos = StringInStr($oInput.innerHTML, String($s_word)) if (Number($iPos) > 0) AND (@error == 0) Then ConsoleWrite("I FOUND IT...! " & String($s_word) & @CRLF) $sHTML = _IEBodyReadHTML($oIE3) $_lat_look = 0 $_lng_look = 0 $_end_look = 0 ;default $_lat_look = StringInStr(String($sHTML), "lat:") if Number($_lat_look) <> 0 Then $_lng_look = StringInStr(String($sHTML), "lng:") if Number($_lng_look) <> 0 Then $_end_look = StringInStr(String($sHTML), "}") if Number($_end_look) <> 0 Then ConsoleWrite("HTML BODY: " & $sHTML & @CRLF) $_old_lat = String(StringMid(String($sHTML), $_lat_look, ($_lng_look - $_lat_look))) $_old_lng = String(StringMid(String($sHTML), $_lng_look, ($_end_look - $_lng_look))) ConsoleWrite("$_old_lat: " & $_old_lat & @CRLF) ConsoleWrite("$_old_lng: " & $_old_lng & @CRLF) $_new_lat = "lat: " & String("-34.397") & ", " $_new_lng = "lng: " & String("150.644") & "}; " ConsoleWrite("...new lat is: " & String($_new_lat) & " new lng is: " & String($_new_lng) & @CRLF) $_LOOK = StringReplace($_old_lat, 1, String($_new_lat)) $_LOOK2 = StringReplace($_old_lng, 1, String($_new_lng)) ConsoleWrite("$_LOOK: " & $_LOOK & "$_LOOK2: " & $_LOOK2 & @CRLF) EndIf ;'$_end_look' NOT "0"... $iPos = -1 EndIf ;'String($s_word)' was found in the collection '$oInputs' EndIf ;'$iPos' is "-1" Next  
        I am having trouble trying to replace the line in the HTML ($sHTML variable in my example) that contains the "lat:" and "lng:" information.  I figure if I can replace that line everything else remains the same, and in theory, the map should cycle to display a map with the new latitude and longitude coordinates...I hope. 
        I have attempted to write the $sHTML to a text document and then use '_IEBodyWriteHTML' to read it back into the webpage HTML however that is not working.  There must be an easier method to accomplish this...what am I missing here...?  Any thoughts greatly appreciated.  Regards.       
×