如何在继续脚本之前等待网页完全加载? [英] How to wait for webpage to fully load before proceeding script?

查看:44
本文介绍了如何在继续脚本之前等待网页完全加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在执行脚本之前如何等待网页完全加载?

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

我知道如果您希望它等待 4 秒,您可以使用延迟 4,但这还不够安全.

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

在 VBA 中,您有一个始终有效的简单代码,它是这样的:

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

在 mac 上使用 safari for applescript 的等价物是什么?

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.

我只想要 100% 完美的上述版本.

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

提前致谢:)

推荐答案

您可以通过从 Safari 或 Chrome 调用document.readyState"来访问页面的就绪状态.

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

这篇关于如何在继续脚本之前等待网页完全加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆