[Func] _IELoadWaitComplete

Partagez des fonctions et des UDF AutoIt.
Règles du forum
.
Répondre
Avatar du membre
timmalos
Niveau 11
Niveau 11
Messages : 1970
Enregistré le : dim. 18 mai 2008 15:16
Status : Hors ligne

[Func] _IELoadWaitComplete

#1

Message par timmalos »

Bonjour à tous. Je vous propose une fonction de mon cru pour corriger la faiblesse de _IELoadWait au niveau des pages chargées dynamiquement par Javascript.
En effet on voit de plus en plus de site qui utilisent Javascript, poussant parfois jusqu'à generer le code de la page entièrement via Javascript. Seulement ce processus est asynchrone et Internet Explorer n'a aucun moyen 'direct' [Et croyez moi, j'ai cherché :lol: ]de nous dire si la page est entièrement chargée ou pas, et si l’élément qui vous intéresse (pour remplir un formulaire generé via Ajax par exemple) est présent dans la page. Plutôt que d'y aller à coup de Sleep() vous pouvez utiliser ma fonction :wink:

Ma fonction tente d'approcher le problème au mieux possible dans le cas général , mais ne fonctionne pas pour 100% des cas qui devront malheureusement être gérés à la main.

Son utilisation est simple, elle est identique à _IELoadWait avec un paramètre supplémentaire, le level de sécurité que vous désirez. (Jusqu'à 4, plus vous montez moins vous avez de risque de rendre la main trop tôt plus vous perdez du temps sur les applications simples, à vous de gerer selon l'application)
Pour la majorité des cas, le level 3 suffit amplement.

Petit exemple:

Code : Tout sélectionner

#include <IE.au3>
#include <Date.au3>
$oIE = _IECreate("http://google.fr")
Global $CountDownloads,$StatusTextChangeTime = _NowCalc()
$SinkObject=ObjEvent($oIE,"_IELoadWaitComplete_","DWebBrowserEvents2")
_IELinkClickByIndex($oIE,0)
_IELoadWaitComplete($oIE,4)
MsgBox(0,"","terminé")

Code : Tout sélectionner

; #FUNCTION# ====================================================================================================================
; Name...........: _IELoadWaitComplete
; Description ...: Wait for a browser page with javascript load to complete before return
; Parameters ....: $o_object    - Object variable of an InternetExplorer.Application
;                  $i_level     - Optional: Level of verification before return
;                  $i_delay     - Optional: Milliseconds to wait before checking status
;                  $i_timeout   - Optional: Period of time to wait before exiting function
;                                   (default = 10000 ms aka 10 sec)
; Return values .: On Success   - Returns the level loaded
;                  On Failure   - Returns 0
;
; Author ........: Timothée Malossane
;
; Requires ......: IE.au3 and Date.au3
; Remarks .......: _IELoadWait function is called whatever $i_level is.
;
;                   There are 4 levels of verification :
;                       level 1 : Only Busy property is checked
;                       level 2 : Busy and readyState properties are checked
;                       level 3 : [DEFAULT]Busy, readyState and Download properties are checked. Each download started MUCH BE ended
;                       level 4 : Busy, readyState, Download and StatusText properties are checked. Each download started MUCH BE ended. The statusText must stay the same for 1 second to return ok
;                           Warning : On Level 4 : The function may not return instantly and wait until 1 second more.
;
;                   Dont forget to include those 2 lines in head of your script.
;                       Global $CountDownloads,$StatusTextChangeTime = _NowCalc()
;                       $SinkObject=ObjEvent($oIE,"_IELoadWaitComplete_","DWebBrowserEvents2")
;
;                   This function need _IELoadWaitComplete_DownloadBegin,_IELoadWaitComplete_DownloadComplete and _IELoadWaitComplete_StatusTextChange to work
; ===============================================================================================================================

Func _IELoadWaitComplete(ByRef $o_object ,$i_level = 3, $i_delay = 0 , $i_timeout = 10000)
    Local $ok = 0
    Local $t = TimerInit()
    sleep($i_delay+100)
    _IELoadWait($o_object,0,$i_timeout)
    While ($ok < $i_level And TimerDiff($t)<$i_timeout)
        If (Not $o_object.Busy()) Then
            If(String($o_object.readyState) <> 1) Then
                If $CountDownloads <=0 Then
                    If (_DateDiff('s',$StatusTextChangeTime,_NowCalc()) >= 1) Then
                        $ok = 4
                    Else
                        $ok = 3
                    EndIf
                Else
                    $ok = 2
                EndIf
            Else
                $ok = 1
            EndIf
        Else
            $ok = 0
        EndIf
        sleep(100)
    WEnd
    return $ok
EndFunc

Func _IELoadWaitComplete_DownloadBegin($text)
    $CountDownloads += 1
EndFunc

Func _IELoadWaitComplete_DownloadComplete($text)
    $CountDownloads -= 1
EndFunc
Func _IELoadWaitComplete_StatusTextChange($text)
    $StatusTextChangeTime = _NowCalc()
EndFunc

 
Répondre