&引用;Can';“得不到文件”;错误-Javascript&;苹果书

&引用;Can';“得不到文件”;错误-Javascript&;苹果书,javascript,excel,safari,applescript,applescript-objc,Javascript,Excel,Safari,Applescript,Applescript Objc,我的Applescript在Safari中(偶尔)遇到这个错误 Result: error "Safari got an error: Can’t get document \"DOC TITLE\"." number -1728 from document "DOC TITLE" 我认为这是因为页面没有加载,但我有一个Javascript在继续之前检查“complete”,还有一个try语句延迟另一秒出错 然而,我仍然遇到这个问题。这似乎是相当随机的 有什么建议吗 Apples

我的Applescript在Safari中(偶尔)遇到这个错误

Result:
   error "Safari got an error: Can’t get document \"DOC TITLE\"." 
   number -1728 from document "DOC TITLE"
我认为这是因为页面没有加载,但我有一个Javascript在继续之前检查“complete”,还有一个try语句延迟另一秒出错

然而,我仍然遇到这个问题。这似乎是相当随机的

有什么建议吗

Applescript(整个Tell语句):

tell application "Safari"
set the URL of the front document to searchURL
delay 1
repeat
    if (do JavaScript "document.readyState" in document 1) is "complete" then exit repeat
    delay 1.5 -- wait a second before checking again
end repeat
try
    set doc to document "DOC TITLE"
on error
    delay 1
    set doc to document "DOC TITLE"
end try
set theSource to source of doc
set t to theSource
end tell

您的代码中存在缺陷。首先检查readyState,如何知道正在检查正确文档的readyState?它可能是以前的文档。延迟1将显著缩短机会,但仍然不安全。我的第一个建议是在检查页面标题之后检查readyState

然后,当检查页面标题时,也会产生错误,我们有可能与上一页的页面标题匹配(如果是同一页,或者至少有相同的标题)。为了确保我们通过标题等待正确的页面,最简单的方法是首先将页面设置为“about:blank”

另一件事,正如你所看到的,我所做的是,每个阶段不再等待超过20秒的页面加载。以下是一种更可控的加载页面的方式:

tell application "Safari"
    -- first set the page to "about:blank"
    set the URL of the front document to "about:blank"
    set attempts to 1
    repeat until "about:blank" is (name of front document)
        set attempts to attempts + 1
        if attempts > 20 then -- creating a timeout of 20 seconds
            error "Timeout while waiting for blank page" number -128
        end if
        delay 1
    end repeat

    -- now we start from a blank page and open the right url and wait until page is loaded
    set the URL of the front document to searchURL
    set attempts to 1
    repeat until "DOC TITLE" is (name of front document)
        set attempts to attempts + 1
        if attempts > 20 then -- creating a timeout of 20 seconds
            error "Timeout while waiting for page" number -128
        end if
        delay 1
    end repeat

    -- now check the readystate of the document
    set attempts to 1
    repeat until (do JavaScript "document.readyState" in document 1) is "complete"
        set attempts to attempts + 1
        if attempts > 20 then -- creating a timeout of 20 seconds
            error "Timeout while waiting for page" number -128
        end if
        delay 1
    end repeat

    -- page should be loaded completely now
    set doc to document "DOC TITLE"
    set theSource to source of doc
    set t to theSource
end tell 

添加超时的问题是,此脚本正在提交表单,每次提交表单时都要向我收费。因此,设置一个超时时间可能会增加我在同一次提交中被收取两倍费用的机会。另外,我刚刚尝试了上面的建议,它使这个脚本运行的时间增加了一倍。我怀疑是延迟造成的。你可以延长超时时间,缩短延迟时间来提高性能,所以我不明白为什么这是一个问题。然而,这个脚本的全部原因是等待页面完全加载,很明显,这需要更长的时间。然而,在一个内部页面上,我已经测试了这个脚本,脚本在不到一秒钟的时间内运行,延迟永远不会被调用。所花费的时间是由您的机器、页面的复杂性、连接和服务器速度造成的。