2014-06-30 91 views
3

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

我知道你可以使用延迟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 

什么是这个在Mac上等价的Safari的AppleScript的?

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

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

通过使用Safari或Chrome叫“document.readyState”谢谢你提前:)

+1

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

回答

3

您可以访问一个页面的就绪状态。

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 
+5

这不适用于10.6.8版本的Safari 5.1.10(6534.59.10)。它说完成,但网页仍然在safari中加载。 – Vic

+0

我在最后加了2秒的额外延迟。它似乎需要执行脚本的下一部分。 – Matts

1

谷歌搜索了一下,发现这个代码适用于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

和它的工作!

1

我做了这种方式:

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 
相关问题