Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wait for webpage to fully load before proceeding script?

Tags:

applescript

How do you wait for a webpage to fully load before proceeding the script?

I know you can use delay 4 if you would want it to wait 4 seconds but this is not safe enough.

In VBA you have a simple code that always works and it goes like this:

Dim x As String
x = "https://na6.salesforce.com/p/attach/NoteAttach?pid=" & Range("T34").Value & "&parentname=" & Range("T35").Value & "&retURL=%2F0018000000sKz8K%3Fpb0%3Dtrue"

   Set IE = CreateObject("InternetExplorer.Application")
    IE.Navigate (x)
    IE.Visible = True
    SetParent IE.Hwnd, Application.Hwnd

Do
DoEvents
Loop Until IE.READYSTATE = 4

What would be the equivalent of this on mac with safari for applescript?

I´ve seen many versions on the net but only workarounds similar to the delay or for a specify value to return or to count words on the page and so on.

I simply want the above version which is 100% perfect.

Thank you in advance:)

like image 256
tnuis Avatar asked Sep 01 '25 16:09

tnuis


1 Answers

You can access the ready state of a page by calling "document.readyState" from either Safari or Chrome.

tell application "Safari"
    if not (exists document 1) then reopen
    tell current tab of window 1 to set URL to "https://stackoverflow.com/questions/24500011/how-to-wait-for-webpage-to-fully-load-before-proceeding-script"

    set the_state to missing value
    repeat until the_state is "complete"
        set the_state to (do JavaScript "document.readyState" in document 1)
        log "busy"
        delay 0.2
    end repeat
    log "complete"
end tell
like image 168
adayzdone Avatar answered Sep 04 '25 05:09

adayzdone