【问题标题】:Target a Chrome Extension using AppleScript使用 AppleScript 定位 Chrome 扩展程序
【发布时间】:2020-07-03 07:59:41
【问题描述】:

我遇到了一些障碍,我可以提出建议。作为我正在编写的脚本的一部分,我的目标是 Chrome 浏览器中的一个扩展程序,以获取网站的全页屏幕截图。我已经能够使用辅助功能检查器识别元素,但我不知道如何单击该元素。我已经尝试了几种附加代码的变体,但运气不佳。

tell application "System Events"
tell process "Google Chrome"
    tell group "Extensions" of group "BBC - Homepage - Google Chrome" of window "BBC - Homepage - Google Chrome"
        click (first button where its accessibility description = "Full Page Screen Capture")
    end tell
end tell

说完

还有

tell application "System Events"
delay 1
tell process "Google Chrome"
    click UI element "Full Page Screen Capture" of group "Extensions" of group "BBC - Homepage - Google Chrome" of window "BBC - Homepage - Google Chrome"
end tell

说完

另外:我可以针对 Extensions 组中的其他元素(即 Window Resizer),但这个不想合作。

欢迎提出任何建议。提前谢谢你。

【问题讨论】:

  • 尝试不同的扩展或通过运行chrome --remote-debugging-port=9222(或不同的端口)使用CDP,然后使用任何CDP客户端发送Page.captureScreenshot(a random example),截图高度限制为16384px IIRC .不要忘记事先完全退出 Chrome。

标签: google-chrome google-chrome-extension applescript


【解决方案1】:

首先,我们在谷歌浏览器中打开一个空白的新标签。较少的 UI 元素有利于分析。然后我们运行下面的代码(XXX):

tell application "Google Chrome"
    activate
    delay 0.3
end tell

tell application "System Events"
    tell front window of (first application process whose frontmost is true)
        set uiElems to entire contents
    end tell
end tell

在文本输出中搜索“screen”,我们看到这样一行:

“New”组“Extensions”组的“Full Page Screen Capture”按钮 Tab - Google Chrome”的窗口 “New Tab - Google Chrome”的 应用程序“谷歌浏览器”,

所以我们知道

按钮在组中(扩展),

组(Ext...)在组(新标签...)中,

组(新标签...)在窗口中...

所以我们测试下面的代码:

tell application "Google Chrome"
    activate
    delay 0.3
end tell

tell application "System Events"
    tell process "Google Chrome"
        tell window "New Tab - Google Chrome"
            tell group "New Tab - Google Chrome"
                tell group "Extensions"
                    tell button "Full Page Screen Capture"
                        click
                        --perform action "AXPress"
                    end tell
                end tell
            end tell
        end tell
    end tell
end tell

它有效。但是我们在代码中看到了活动标签“新标签 - 谷歌浏览器”的名称,所以如果我们转到另一个标签,代码就不起作用。 所以我们需要知道如何获取前台窗口的活动标签的名称。我做了很多测试。最后我找到了这个page的答案。

osascript -e 'tell app "google chrome" to get the title of the active tab of window 1'

然后我把它放到我的AppleScript中,测试下面的代码:

tell application "Google Chrome"
    activate
    delay 0.3
end tell

tell application "System Events"
    tell process "Google Chrome"
        set theTitle to do shell script "osascript -e 'tell app \"google chrome\" to get the title of the active tab of window 1'" as text
        set theTitle to theTitle & " - Google Chrome"
        --note the example title is "New Tab - Google Chrome". Don't forget to attach " - Google Chrome"
        tell window theTitle
            tell group theTitle
                tell group "Extensions"
                    tell button "Full Page Screen Capture"
                        --click
                        perform action "AXPress"
                    end tell
                end tell
            end tell
        end tell
    end tell
end tell

大多数时候,这个版本的代码可以工作,但有时它不起作用。我在此选项卡上运行代码(XXX),有趣的是,按钮名称从“全页屏幕捕获”更改为“全页屏幕捕获”

可以访问这个站点”注意它在“Capture”之后插入了一个“\r”。所以我们知道窗口的标题可能会改变,按钮名称可能会改变。所以我尝试制作一个处理程序。测试下面的代码:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Google Chrome"
    activate
    delay 0.3
end tell

try
    set buttonName to "Full Page Screen Capture"
    fullPageScreenshot(buttonName)
on error
    set buttonName to "Full Page Screen Capture
Has access to this site"
    --note here is a "\r"
    fullPageScreenshot(buttonName)
end try

on fullPageScreenshot(buttonName)
    tell application "System Events"
        tell process "Google Chrome"
            set theTitle to do shell script "osascript -e 'tell app \"google chrome\" to get the title of the active tab of window 1'" as text
            delay 0.2
            set theTitle to theTitle & " - Google Chrome"
            --note the example title is "New Tab - Google Chrome". Don't forget to attach " - Google Chrome"
            tell window theTitle
                tell group theTitle
                    tell group "Extensions"
                        tell button buttonName
                            --click
                            perform action "AXPress"
                        end tell
                    end tell
                end tell
            end tell
        end tell
    end tell
end fullPageScreenshot

现在这个版本的代码,我测试了20多次,总是可以的。我不经常使用这个扩展。我的谷歌浏览器版本是 80.0.3987.149 (Official Build) (64-bit);和 MacOS 10.12.6。请让我知道此代码是否适用于您的机器。

【讨论】:

  • 感谢您的回答!非常非常有帮助且内容丰富。我不知道您将初学者的执行操作称为“AXPress”。你剩下的答案只是额外的奖励。
猜你喜欢
  • 2011-09-27
  • 2018-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多