简体   繁体   English

使用Applescript的浏览器自动化

[英]Browser automation using Applescript

I have just started working with applescript and am trying web automation. 我刚刚开始使用applescript,正在尝试进行网络自动化。 My task is to launch the browser, sign in and then click on certain links. 我的任务是启动浏览器,登录,然后单击某些链接。

While my script is able to launch browser and sign in, I am not able to automate the mouse click on links or buttons. 虽然我的脚本可以启动浏览器并登录,但我无法自动在链接或按钮上单击鼠标。 I have tried a number of solutions available on the web(including those on stackoverflow) but nothing seems to work. 我已经尝试了许多在Web上可用的解决方案(包括stackoverflow上的解决方案),但是似乎没有任何效果。 The error message I am getting is 'value missing' 我收到的错误消息是“值缺失”

Since I am a completely new at applescript, I am not completely sure whether this can be done or not. 由于我是applescript的新手,因此我不确定是否可以这样做。 Have a look at my script: (The comments "(*or: *)" are the alternates I have tried). 看一下我的脚本:(注释“(*或:*)”是我尝试过的替代方法)。

    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

Although you'll have to heavily customize this for your situation, this is the code I'm using to log into AT&T's website and retrieve data usage information, then log out. 尽管您必须针对自己的情况进行大量自定义,但这是我用来登录AT&T网站并检索数据使用信息然后注销的代码。 The crucial part here is the wait() function. 这里的关键部分是wait()函数。 This is much better and more reliable than arbitrary delays. 这比任意延迟要好得多并且更可靠。 It checks for the status of Safari's reload button, and continues when it indicates the webpage is done loading. 它检查Safari的“重新加载”按钮的状态,并在指示网页加载完成时继续。 You can use Safari's web inspector to get element IDs and stuff. 您可以使用Safari的网络检查器来获取元素ID和内容。 If you need any further help making this work, don't hesitate to ask! 如果您需要进一步的协助以完成这项工作,请随时询问!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM