Jump to content
aloalo

Help with loop returning an element

Recommended Posts

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

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

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."

11

Signature last update: 2018-10-31

Share this post


Link to post
Share on other sites

You can also do this instead...

If IsObj($bDone) Then
   Return $bDone
EndIf

 

Edited by jdelaney

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

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

    • By SkysLastChance
      What would be the best way to grab the last digits of this <span>? One of the problems I know I am going to have is sometimes it will be 1 digit other times it might be 3. 

      I am trying to get the list of spans and I get this error.

       
      $oInputs = _IETagNameGetCollection($oIE, "span") $sTxt = "" For $oInput In $oInputs     $sTxt &= $oInput.Innertext & @CRLF Next MsgBox($MB_SYSTEMMODAL, "Form Input Type", "Form: " & $oInput.form.name & @CRLF & @CRLF & "         Types :" & @CRLF & $sTxt)  
    • By matwachich
      Hi guys!
      A pretty simple UDF to convert HTML to PDF using wkHTMLtoPDF.
      It uses the C API of the tool (DLL), so no external process, no ActiveX or COM sh*t.
      See the example, and the documentation of wkHTMLtoPDF.
      Cheers
      https://github.com/matwachich/wkhtmltopdf-au3
    • By Colduction
      Hello AutoIt Scriptwriters! 
      I want to read https based site that it's address is: Soft98 (https://soft98.ir/)
      I've tried with "_INetGetSource", "BinaryToString(InetRead)" and "InetRead" but none of them don't help me
       
      How can i get this site html source code without opening IE Windows? 
       
    • By Viszna
      Hello.
      I create a function that saves the log to an html file.
      File html are included picture (conversion to Base64)
      Everything works OK.
      But I do the actions:
      screenshot to the png file (smaller than bmp) per disk (  _ScreenCapture_Capture(@ScriptDir & "\screenshot.png")  ) convert image from disk to base64 I need help to optimize the script:
      - screenshot to memory (do not save to disk) - convert this object to png in memory - invoking the conversion of png image from memory to base64 The first step _ScreenCapture_Capture("") create handle to an HBITMAP in memory
      How to convert image in memory to png?
      How to use Func _ConvertToBase64
      I attach my code
      #include <ScreenCapture.au3> #include <Date.au3> Global $RaportFileName = @YEAR & "-" & @MON & "-" & @MDAY & "_" & @HOUR & "_" & @MIN & "_" & @SEC & ".html" $text = "This is first line text" & @CRLF &"and this is next line" _Raport($text, 1) FileWrite(@ScriptDir & "\" & $RaportFileName, "</pre></html>") ; #FUNCTION# ==================================================================================================================== ; Name ..........: _Raport ; Description ...: ; Syntax ........: _Raport($sText1[, $Screen = 0]) ; Parameters ....: $sText1 - a string value. ; $Screen - [optional] an unknown value. Default is 0. ; 0 - Default - do not screenshot ; 1 - added screenshot full desktop ; Return values .: None ; Author ........: Your Name ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _Raport($sText1, $Screen=0) Local $sText = "" Local $sHead = "" ; Define HTML file header and style $sHead = '<html>' & @CRLF & '<head><meta charset="utf-8"></head>' & @CRLF $sHead = $sHead & '<style>img{border:3px solid #FF0000;}</style>' & @CRLF $sHead = $sHead & '<style>pre{font-family: monospace;}</style>' & @CRLF $sHead = $sHead & '<style>pre{font-size: large;}</style>' & @CRLF $sHead = $sHead & '<pre>' & @CRLF If NOT FileExists(@ScriptDir & "\" & $RaportFileName) Then ; If file Raport not exist then create FileOpen(@ScriptDir & "\" & $RaportFileName, 258) FileWrite(@ScriptDir & "\" & $RaportFileName, $sHead) EndIf If StringInStr($sText1, @CRLF) > 0 Then ; @CRLF (ENTER) change the @CRLF and 11 space (indentation on width "[GG:MM:SS] ") $sText1 = StringReplace($sText1, @CRLF, @CRLF & '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;') EndIf $sText = $sText & $sText1 If $Screen <> 0 Then _ScreenCapture_Capture(@ScriptDir & "\screenshot.png") $sText = $sText & @CRLF & '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' & '<img src="data:image/png;base64,' & _ConvertToBase64(@ScriptDir & "\screenshot.png") & '"/>' EndIf FileWrite(@ScriptDir & "\" & $RaportFileName, "[" & _NowTime(5) & "]&nbsp;" & $sText & "<br><br>"&@CRLF) ; write to file Raport EndFunc Func _ConvertToBase64($fFile) ;Xroot 2011 ;ClipPut("") ;$FN=@ScriptDir & "\screenshot.png" $FN=$fFile $dat=FileRead(FileOpen($FN,16)) $objXML=ObjCreate("MSXML2.DOMDocument") $objNode=$objXML.createElement("b64") $objNode.dataType="bin.base64" $objNode.nodeTypedValue=$dat ClipPut("") $Wynik = "" ;ClipPut($objNode.Text) $Wynik = $objNode.Text Return $Wynik EndFunc P.S.
      Excuse me my not good English.
    • By Arlen
      I need to find a way to get the HTML from a website that has HTTPS. This is what I have tried:
      - WinHttp (Only worked on HTTP NOT HTTPS)
      - InetGet and _INetGetSource Function (Not working for HTTPS)
      - IE.au3 (Does work but it's too slow for my needs)
      If anybody can point me on the right direction, I would really appreciate it.
       
×
×
  • Create New...