首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Applescript的浏览器自动化

使用Applescript的浏览器自动化
EN

Stack Overflow用户
提问于 2015-07-28 22:35:27
回答 1查看 1.7K关注 0票数 0

我刚刚开始使用applescript,正在尝试web自动化。我的任务是启动浏览器,登录,然后点击某些链接。

虽然我的脚本能够启动浏览器和登录,但我不能自动化鼠标点击链接或按钮。我在网上尝试了许多可用的解决方案(包括stackoverflow上的解决方案),但似乎都不起作用。我收到的错误消息是'value missing‘

由于我是applescript的新手,我不能完全确定这是否可以做到。看看我的脚本:(注释“(*或:*)”是我尝试过的备选方案)。

代码语言:javascript
复制
    set userID to "username"
    set pwd to "password"

    tell application "Safari"
    activate

    make new document with properties {URL:"http://example.com"}  (*or:         
    open location "example.com"*)

    tell application "System Events"
    delay 4
    keystroke tab

    keystroke userID
    delay 1
    keystroke tab

    keystroke pwd
    delay 1
    keystroke return
    delay 3

    tell application "Safari"  
   (*or: tell application "Safari" to tell active tab of window 1*)

    do JavaScript "$( 'apps-button .switcher-button switcher-button-   
    left :equ(0)').click()" in front document
    (*or: do JavaScript "document.getElementByID('apps-  
     button').click()"*)

    end tell
    end tell
    end tell
EN

回答 1

Stack Overflow用户

发布于 2015-07-29 04:42:28

尽管您必须根据您的情况对其进行大量定制,但这是我用来登录AT&T网站并检索数据使用信息,然后注销的代码。这里的关键部分是wait()函数。这比任意延迟要好得多,也更可靠。它检查Safari的reload按钮的状态,并在指示网页已完成加载时继续。您可以使用Safari的web检查器来获取元素ID和其他内容。如果你需要更多的帮助,请不要犹豫,尽管问!

代码语言:javascript
复制
set username to "username"
set _password to "password"

tell application "Safari"
    set new_document to make new document with properties {URL:"https://www.att.com/olam/loginAction.olamexecute"}
    tell me to wait()
    tell tab 1 of window 1
        do JavaScript "document.getElementById('userID').value = \"" & username & "\""
        do JavaScript "document.getElementById('password').value = \"" & _password & "\""
        do JavaScript "document.getElementById('LoginButton').children[0].click()"
        tell me to wait()
        set URL to "https://www.att.com/olam/displayUsageLanding.myworld?reportActionEvent=A_UMD_CURRENT_USAGE_SUMMARY"
        tell me to wait()
        display alert (do JavaScript "document.getElementById('WebUsage1').children[0].children[0].innerHTML")
        do JavaScript "document.getElementById('ge5p_z2-user-auth').click()"
        tell me to wait()
    end tell
    close window 1
end tell

on wait()
    delay 1
    tell application "System Events"
        tell application process "Safari"
            set shouldExitRepeat to false
            repeat
                try
                    repeat with curButton in (buttons of text field 1 of group 2 of toolbar 1 of window 1)
                        if name of curButton is equal to "reload" then
                            set shouldExitRepeat to true
                        end if
                    end repeat
                    if shouldExitRepeat then
                        exit repeat
                    end if
                end try
                delay 1
            end repeat
        end tell
    end tell
end wait
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31679447

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档