【问题标题】:"Can't Get Document" Error - Javascript & Applescript“无法获取文档”错误 - Javascript 和 Applescript
【发布时间】:2014-04-20 13:04:34
【问题描述】:

我的 Applescript 在 Safari 中(偶尔)遇到此错误。

Result:
   error "Safari got an error: Can’t get document \"DOC TITLE\"." 
   number -1728 from document "DOC TITLE"

我认为这是因为页面没有加载,但我有一个 Javascript 在继续之前检查“完成”,还有一个 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

【问题讨论】:

    标签: javascript excel safari applescript applescript-objc


    【解决方案1】:

    您的代码中存在缺陷。首先你检查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 
    

    【讨论】:

    • 添加超时的问题是该脚本正在提交表单,并且每次提交时都会向我收费。因此,设置超时可能会增加我为同一提交支付两次费用的机会。
    • 另外,我刚刚尝试了上面的建议,它使该脚本的运行时间加倍。我怀疑是延误。
    • 您可以延长超时时间和缩短延迟时间以提高性能,所以我不明白为什么这是个问题。然而,这个脚本的全部原因是等到页面完全加载,那么很明显它需要更长的时间。然而,在一个内部页面上,我已经对此进行了测试,并且脚本在不到一秒的时间内运行,永远不会调用延迟。所花费的时间是由您的机器造成的,因为页面的复杂程度取决于您的连接和服务器速度。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多