javascript - Applescript 等待页面加载

标签 javascript applescript

在继续执行 applescript 之前,我尝试了各种解决方案来轮询 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

我尝试了网上提供的各种解决方案,但没有成功。任何人都可以提供任何建议来使该代码正常工作吗?

最佳答案

你的一些逻辑是错误的,令人困惑。另外,最佳实践是通过嵌套 .contains 让 javascript 传递 bool 值。这是一个使用三种方法来测试页面是否已加载的脚本:

一个 javascript document.readyState

一个 javascript document.contains

一个简单的 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

关于javascript - Applescript 等待页面加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33405842/

相关文章:

javascript - 只读 asp 下拉列表

javascript - 仅使用类名删除 div

objective-c - 从 Cocoa 应用程序访问 Safari 文档的 DOM

macos - 使用 AppleScript 阅读 iMessage

applescript - 使用 AppleScript 或终端在 OS X 中重命名窗口标题

applescript - 如何从命令行而不是 Finder.app 预览文件内容

macos - 在 2x-click 上运行 applescript

javascript - C# WebSocket - 握手期间客户端没有响应

JavaScript 函数不适用于在函数之前声明的变量

javascript - Liferay 7 上的 Portlet 部署问题