【问题标题】:How to wait for webpage to fully load before proceeding script?如何在继续脚本之前等待网页完全加载?
【发布时间】:2014-08-21 09:47:44
【问题描述】:

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

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

在 VBA 中,您有一个始终有效的简单代码,如下所示:

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

在带有 Safari for applescript 的 mac 上,这相当于什么?

我在网上看到过很多版本,但只有类似于延迟或返回指定值或计算页面上的字数等解决方法。

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

提前谢谢你:)

【问题讨论】:

  • Safari / Chrome / Firefox 没有“就绪状态”状态,您可以通过 js 或 applescript 从它们那里获得。我在“重复直到将值加载到站点内容”方法上取得了巨大的成功。我记得在某个时候在堆栈上发布了一个作为答案。

标签: applescript


【解决方案1】:

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

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

【讨论】:

  • 这对我在 10.6.8 和 Safari 版本 5.1.10 (6534.59.10) 上不起作用。它说完成,但页面仍在 safari 中加载。
  • 我在最后添加了 2 秒的额外延迟。它似乎需要这个来执行脚本的下一部分。
【解决方案2】:

Google 了一下,发现这段代码适用于 Snow Leopard 上的 Safari 版本 5.1.10 (6534.59.10)。

来自http://macscripter.net/viewtopic.php?id=35176

tell application "Safari"
   activate
   repeat until SafariWindowHasLoaded(1) of me is true
   end repeat
   beep
end tell

on SafariWindowHasLoaded(inWindowIndex)
   tell application "System Events" to ¬
       tell application process "Safari"    
           set theStatusText to name of static text 1 of group 1 of window inWindowIndex as text
           if theStatusText begins with "Contacting" or ¬
               theStatusText begins with "Loading" or ¬
               theStatusText begins with "Waiting" then
               set theReturnValue to false
           else
               set theReturnValue to true
           end if
       end tell
   return theReturnValue
end SafariWindowHasLoaded

我遇到的唯一问题是该脚本适用于旧版本的 Safari,并且状态栏元素与我的组不同。

http://hints.macworld.com/article.php?story=20071015132722688 建议改成

static text 1 of group 2

它成功了!

【讨论】:

    【解决方案3】:

    我是这样做的:

    tell document 1 of application "Safari"  
      set URL to ""  
      set URL to "/slowly loading page/"  
      repeat until name is not "about:blank"  
      end repeat  
    end tell
    

    或者,更简单:

    tell front document of application "Safari"  
      set URL to "/slowly loading page/"  
      repeat until length of (source as text) is not 0  
      end repeat  
    end tell
    

    此外,没有必要立即这样做:

    repeat until length of (source as text) is not 0  
      delay 0.5  
    end repeat
    

    【讨论】:

      猜你喜欢
      • 2017-07-24
      • 1970-01-01
      • 1970-01-01
      • 2011-03-19
      • 2014-07-05
      • 2023-03-14
      • 1970-01-01
      • 2021-06-26
      • 2014-01-15
      相关资源
      最近更新 更多