我刚刚开始使用applescript,正在尝试web自动化。我的任务是启动浏览器,登录,然后点击某些链接。
虽然我的脚本能够启动浏览器和登录,但我不能自动化鼠标点击链接或按钮。我在网上尝试了许多可用的解决方案(包括stackoverflow上的解决方案),但似乎都不起作用。我收到的错误消息是'value missing‘
由于我是applescript的新手,我不能完全确定这是否可以做到。看看我的脚本:(注释“(*或:*)”是我尝试过的备选方案)。
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发布于 2015-07-29 04:42:28
尽管您必须根据您的情况对其进行大量定制,但这是我用来登录AT&T网站并检索数据使用信息,然后注销的代码。这里的关键部分是wait()函数。这比任意延迟要好得多,也更可靠。它检查Safari的reload按钮的状态,并在指示网页已完成加载时继续。您可以使用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 waithttps://stackoverflow.com/questions/31679447
复制相似问题