Jump to content

FF.au3: _FFWaitWhileElement() --- A Function to pause script until Element CANNOT be found on page


noellarkin
 Share

Recommended Posts

This is for those pages that seem to load when you use _FFOpenURL(), but they have a text saying "please wait while we load your page..." but according to _FFOpenURL() the page has loaded.  An example seen in popular sites is the Cloudflare page saying "Checking your browser before...". It's the opposite of pausing a script until an element is visible on a page. It takes Elements in the form of id, xpath or text search.

Global $BrowserPort = 4242
Global $TimeOut = 60000
Global $LocalHost = "127.0.0.1"

    Func _FFWaitWhileElement($thiselement, $elementtype = "xpath", $timeoutms = 60000)

        _FFConnect($LocalHost, $BrowserPort, $TimeOut)

        Local $Element
        Local $ElementFound = 1
        Local $TimeoutCountdown = 0
        
        While $ElementFound <> 0 And $TimeoutCountdown < $timeoutms
        
            If $elementtype = "xpath" Then
                $Element = _FFXPath($thiselement)
            EndIf
            
            If $elementtype = "id" Then
                Local $ConstructXPath = ".//*[@id='" & $thiselement & "']"
                $Element = _FFXPath($ConstructXPath)
            EndIf
            
            ; MsgBox(0, "", $Element)
            
            Local $ElementXPathTextContent = _FFCmd("FFau3.xpath.textContent")
            ; MsgBox(0, "", $ElementXPathTextContent)
            Local $ElementXPathInnerHTML = _FFCmd("FFau3.xpath.innerHTML")
            ; MsgBox(0, "", $ElementXPathInnerHTML)
            If $ElementXPathTextContent = "_FFCmd_Err" Or $ElementXPathInnerHTML = "_FFCmd_Err" Then
                $ElementFound = 0
                Else
                $ElementFound = 1
            EndIf
        
            If $elementtype = "text" Then
                $ElementFound = _FFSearch($thiselement)
                ; MsgBox(0, "", $ElementFound)
            EndIf

            $TimeoutCountdown += 1000
            _FFDisConnect()
            _FFConnect($LocalHost, $BrowserPort, $TimeOut)
        WEnd
        Return $ElementFound

    EndFunc

_FFConnect($LocalHost, $BrowserPort, $TimeOut)
_FFOpenURL("https://www.site.com/page", True)
_FFWaitForElement("Please Wait...","text", 60000)
MsgBox(0,"","Page finished loading")

It's pretty simple, hope it helps some of you who are working with FF.au3 :)

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