我是 AppleScript 新手,遇到了以下问题:
我正在为 Chrome 创建一个脚本,我想在其中登录某个网站并为我检查一些内容。现在,我只需要检查我是否确实路由到了正确的 URL(有时我已经登录)或者是否需要登录(如果系统已自动将我注销)。如果我已注销,我只想单击“登录”按钮(我的凭据自动填充)。如果我已经登录,那么我将继续执行脚本的其余部分。
这就是我现在的位置:
activate application "Google Chrome"
tell application "Google Chrome"
open location "URL"
--Check if I have to login or not
--If I do, click the button "login"
--If not, proceed.
--The rest.
end tell
我正在考虑: 1.) 检查我是否位于正确的 URL,如果不正确,请单击登录按钮 或 2.) 检查登录按钮是否存在,如果存在则单击它
我希望得到一些关于哪种方式更有效以及如何实现它的指导。现在正在努力寻找正确的命令...谢谢!
最佳答案
这是一个脚本,用于打开该位置,等待加载完成,检查 URL 中是否存在特定字符串,如果存在,则使用 JavaScript 按下按钮。
tell application "Google Chrome"
activate
open location "http://somesite.com"
set theTab to tab 1 of window 1
repeat
if (loading of theTab) is false then exit repeat
end repeat
set theURL to URL of theTab
if theURL contains "login" then
execute theTab javascript "document.getElementById('login-button').click();"
end if
end tell
您需要更改脚本在 URL 中检查的字符串。 您还需要了解按钮 ID。只需浏览页面的源代码(菜单 View /开发人员/)即可找到该内容,并将其切换为“登录按钮”。查找如下行:
<button class="login submit" value="Login now" type="submit" id="login-button">
关于javascript - 如何检查 AppleScript (Chrome) 中的 URL 是否正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24643251/