【问题标题】:How to enable "Allow Remote Automation" in Safari programmatically如何以编程方式在 Safari 中启用“允许远程自动化”
【发布时间】:2017-05-16 16:15:35
【问题描述】:

我正在使用 macOS Sierra 10.12.4 和 safari 版本 10.1

我需要以编程方式在 Safari 的“开发”选项卡中启用“允许远程自动化”选项。

我可以运行以下命令来更改 ~/Library/Preferences 中的 com.apple.Safari.plist 文件并完美启用“开发”菜单。

`defaults write com.apple.Safari IncludeDevelopMenu -bool true` 

但是我没有找到任何启​​用“允许远程自动化”的选项

知道哪个 plist 包含该信息吗?

【问题讨论】:

    标签: ios selenium selenium-webdriver safari safaridriver


    【解决方案1】:

    使用您描述的方法无法切换设置。

    从 Safari 11 开始,您可以使用 --enable 命令行选项强制 safaridriver 进行身份验证。验证后,将设置此菜单项。这还将缓存其余登录会话的身份验证。 safaridriver 的后续调用(例如,由 Selenium 库)将不需要进一步设置。

    【讨论】:

    • 那么正确的命令是sudo defaults write com.apple.Safari IncludeDevelopMenu -enable true 吗?
    • 抱歉,过激了。我相信是/usr/bin/safaridriver --enable
    【解决方案2】:

    如果无法通过修改 plist 也可以使用 AppleScript 来完成。为此,首先从 Safari 首选项启用开发,然后从“开发”菜单中选择“允许远程自动化”。 这是我为启用“允许远程自动化”而编写的 AppleScript(这涵盖了上述两个步骤)。

    tell application "Safari" to activate
    delay 2
    tell application "System Events"
        tell application process "Safari"
            keystroke "," using command down
            set frontmost to true
            tell window 1
                click button "Advanced" of toolbar 1
                delay 2
                set theCheckbox to checkbox 4 of group 1 of group 1 of it
                tell theCheckbox
                    if not (its value as boolean) then click theCheckbox
                end tell
                delay 2
                keystroke "w" using command down
                delay 2
            end tell
            tell menu bar item "Develop" of menu bar 1
                click
                delay 2
                click menu item "Allow Remote Automation" of menu 1
                delay 2
            end tell
        end tell
    end tell
    tell application "Safari" to quit
    

    注意:这里我只在未选中的情况下启用了 safari 偏好设置中的开发菜单。

    希望这会有所帮助..

    【讨论】:

    • 我喜欢 Applescript 的角度——它曾经是“我知道这是可能的”,但在 15 行左右看到它是有启发性的。请注意,这需要使用管理员密码进行手动干预才能授予应用权限。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-11
    • 1970-01-01
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    相关资源
    最近更新 更多