问题描述
我是新来的AppleScript和有这个麻烦:
I'm new to AppleScript and having trouble with this:
我创建了Chrome浏览器的脚本,我想登录到一个网站,并检查我的东西。现在,我只需要检查,如果我实际上路由到正确的URL(有时我已经登录),或者如果我需要登录(如果系统已经自动登录我出去)。如果我退出,我只是想点击登录按钮(我的凭据自动填写)。如果我在我已经登录,那么我就继续执行脚本的其余部分。
I'm creating a script for Chrome where I want to login to a site and check something for me. For now, I just need to check if I'm actually routed to the correct URL (sometimes I'm already logged in) or if I need to login (if the system has logged me out automatically). If I'm logged out, I just want to click the "Login" button (my credentials autofill). If I'm logged in already, then I'll just proceed with the rest of the script.
这是我在哪里:
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)检查登录按钮的presence并点击它,如果它是present
I was thinking of either:1.) checking if I'm at the right URL and then if not, click the login buttonor 2.) check for presence of the login button and click it if it's present
我喜欢某个方向上哪种方式更有效,如何实现它。现在在努力寻找正确的命令...谢谢!
I would love some direction on which way is more efficient and how to accomplish it. Struggling now to find the right commands... Thanks!
推荐答案
下面是打开的位置,等待直到它完成加载脚本,检查特定字符串的URL,如果它的存在,presses一个按钮使用JavaScript。
Here is a script that opens the location, waits til it's done loading, checks the url for a particular string, and if it's there, presses a button using 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。只需浏览源$ C $ C(菜单查看/开发/)页面发现,并切换出的登录按钮。寻找这样一行:
You'll need to change what string the script checks for in the url.You will also need to learn the button ID. Just browse the the source code (Menu View/Developer/) of the page to find that, and switch it out for 'login-button'. Look for a line like:
<button class="login submit" value="Login now" type="submit" id="login-button">
这篇关于如何检查,如果我在的AppleScript正确的URL(铬)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!