如何检查我是否在 AppleScript (Chrome) 中的正确 URL [英] How to check if I'm at the right URL in AppleScript (Chrome)

查看:18
本文介绍了如何检查我是否在 AppleScript (Chrome) 中的正确 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 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.) 检查登录按钮是否存在,如果存在则单击它

I was thinking of either: 1.) checking if I'm at the right URL and then if not, click the login button or 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,如果存在,则使用 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.只需浏览页面的源代码(菜单视图/开发人员/)即可找到它,然后将其切换为登录按钮".寻找如下一行:

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 (Chrome) 中的正确 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆