Applescript正在等待加载页面

Applescript waiting for a page to load

本文关键字:加载 在等待 Applescript      更新时间:2023-09-26

在继续执行appscriptive之前,我已经尝试了各种解决方案来轮询elementID。我宁愿投票,也不愿随意拖延。

set pageNotLoaded to true
    set doForThisID to do JavaScript "document.getElementById(‘thisElementID‘);"
    repeat while pageNotLoaded is true
        try
            if (doForThisID contains "thisElementID") then
                set pageNotLoaded to false
            end if
        end try
    end repeat

我尝试过网上提供的各种解决方案,但都无济于事。有人能为这个代码提供一些建议吗?

您的一些逻辑不正常,会使事情变得混乱。此外,最好的做法是让javascript通过嵌套.contents来传递布尔值

javascript文档.readyState

一个javascript文档。包含

对文档文本进行简单的Safari调用以查找字符串。

您可以更改此脚本以仅使用其中一个。

property testingID : "login"
property testingString : "Username:"
set pageLoaded to false
tell application "Safari"
    repeat while pageLoaded is false
        set readyState to (do JavaScript "document.readyState" in document 1)
        set foundID to (do JavaScript "document.contains(document.getElementById(" & quoted form of testingID & "))" in document 1)
        set pageText to text of document 1
        if (readyState is "complete") and (foundID is true) and (pageText contains testingString) then set pageLoaded to true
        delay 0.2
    end repeat
end tell
display dialog "Ready state of page: " & readyState & return & ¬
    "ID is loaded: " & foundID & return & ¬
    "Test string is loaded: " & testingString