使用Applescript进行浏览器自动化

时间:2015-07-28 14:35:27

标签: javascript macos safari applescript webbrowser-control

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

虽然我的脚本能够启动浏览器并登录,但我无法在链接或按钮上自动点击鼠标。我已经在网上尝试了许多解决方案(包括那些在stackoverflow上的解决方案),但似乎没有任何效果。我得到的错误信息是“缺少价值”

由于我是一个全新的AppleScript,我不完全确定这是否可以完成。看看我的剧本: (注释“(*或:*)”是我试过的替代词。)

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
    var dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"
    var date = dateFormatter.stringFromDate(notification.fireDate!) //app is crashing here.
    NSNotificationCenter.defaultCenter().postNotificationName("RefreshVerseNotify", object: nil,userInfo:["date":date])
}

1 个答案:

答案 0 :(得分:0)

虽然您必须根据自己的情况对此进行大量自定义,但这是我用来登录AT& T&#t的网站并检索数据使用信息然后退出的代码。这里的关键部分是wait()函数。这比任意延迟更好,更可靠。它会检查Safari重新加载按钮的状态,并在指示网页加载完成时继续。您可以使用Safari的Web检查器来获取元素ID和内容。如果您需要任何进一步的帮助来完成这项工作,请不要犹豫!

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