【问题标题】:How to "Wait until web page loaded" in Firefox (applescript)?如何在 Firefox(applescript)中“等待网页加载”?
【发布时间】:2021-10-14 20:29:25
【问题描述】:

来自here 的这段代码运行良好,但在 Firefox 中无法运行。为什么以及如何在 applescript 中修复它?

tell application "Google Chrome"
    repeat until (loading of tab 1 of window 1 is false)
        1 + 1 --just an arbitary line
    end repeat
    loading of tab 1 of window 1 --this just returns final status
end tell

【问题讨论】:

标签: firefox applescript automator


【解决方案1】:

由于 Firefox 不包含特定的 AppleScript 字典,例如Firefox.sdef file,它不被认为是 AppleScript scriptable 与 e.g. Google Chrome,虽然它会响应一些基本的命令,例如 activatequit 等,但它需要 UI 脚本 做你想做的事。

以下示例 AppleScript 代码需要Firefox 87或更高版本,并设置其accessibility.force_disabled 偏好到:-1

  • 首先,在 Firefox 中,在 地址/搜索 组合框中,输入 about:config 并按 enter.

    • 如果适用,请点击接受风险并继续按钮。
  • 搜索偏好名称 文本框中,输入accessibility.force_disabled,然后按enter

  • 单击编辑按钮并将其更改为:-1

  • 点击保存按钮。


这是一个示例,说明我将如何指示 Firefox 打开一个新的窗口 到给定的 URL并等待页面完成加载。

示例 AppleScript 代码:

set theURL to "https://news.google.com/"

tell application "Firefox" to activate
delay 0.5 -- # Value may need to be adjusted if Firefox is closed.

my clickApplicationMenuCommand("Firefox", "File", "New Window")
delay 0.5
my setURLofFirefoxFrontWindowTo(theURL)
my waitForFirefoxPageToFinishLoading()

say "foobar"


--  # Handler(s) #

to clickApplicationMenuCommand(appName, appMenuName, appMenuCommand)
    tell application appName to activate
    delay 0.25
    tell application "System Events" to ¬
        click ¬
            menu item appMenuCommand of ¬
            menu appMenuName of ¬
            menu bar item appMenuName of ¬
            menu bar 1 of ¬
            application process appName
end clickApplicationMenuCommand


to setURLofFirefoxFrontWindowTo(theURL)
    tell application "System Events"
        tell application process "Firefox"
            set the value of UI element 1 of ¬
                combo box 1 of toolbar "Navigation" of ¬
                first group of front window to theURL
            key code 36 --  # enter key
        end tell
    end tell
end setURLofFirefoxFrontWindowTo


to waitForFirefoxPageToFinishLoading()
    --  # Requires Firefox version 87 or newer.
    --  # Requires accessibility.force_disabled set to: -1
    tell application "System Events"
        tell application process "Firefox"
            repeat until exists UI element "Reload" of ¬
                toolbar "Navigation" of group 1 of window 1
                delay 0.1
            end repeat
            repeat while (name of UI elements of ¬
                toolbar "Navigation" of group 1 of ¬
                window 1 whose description is "Reload") ¬
                is not {"Reload"}
                delay 0.1
            end repeat
        end tell
    end tell
end waitForFirefoxPageToFinishLoading

注意事项:

示例 AppleScript 代码,如上所示,在 ma​​cOS Catalina 下的 Script Editor 中进行了测试系统偏好设置 中的 语言和地区 设置设置为 英语(美国)- 主要 并且为我工作没有问题 1.

  • 1 假设 系统偏好设置 > 安全和隐私 > 隐私中的必要和适当的设置已设置/根据需要解决。

使用 Firefox 91.0 版(64 位)测试。

请注意,UI 脚本 非常笨拙且容易失败,尤其是当 OS 和/或应用程序 的版本发生变化时,或者delay commands 是不够的。尽管如此,对于 Firefox,这里所描述的就是它所需要的。



注意:示例 AppleScript 代码就是这样,没有任何包含的错误处理 em> 不包含任何适当的附加错误处理。用户有责任根据需要或需要添加任何错误处理。查看AppleScript Language Guide 中的try statementerror statement。另请参阅Working with Errors。此外,在适当的情况下,可能需要在事件之间使用delay 命令,例如delay 0.5延迟设置得当。

【讨论】:

  • @Bro,请看一下:[当有人回答我的问题时我该怎么办?](stackoverflow.com/help/someone-answers 如果我的回答解决了您的问题并且您倾向于将其标记为接受的答案将不胜感激,谢谢。
猜你喜欢
  • 1970-01-01
  • 2020-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-20
  • 2019-03-30
  • 2013-11-24
  • 1970-01-01
相关资源
最近更新 更多