Applescript / Javascript无法填充Safari输入框



我已经将我的Safari主页设置为具有用户名和密码形式的url。我的脚本应该打开Safari并将用户名设置为一个值。脚本打开safari,显示主页,但没有在输入框中添加文本。

tell application "Safari"
activate
tell window 1
delay 3
do JavaScript "document.getElementById('username').value = 'hello';"
end tell
end tell
<input type="text" name="username" id="username" size="20" style="width: 150px;">

谁能告诉我为什么?

您需要告诉Safari其中,如tab n of window ndocument n

AppleScript<代码/em>:

tell application "Safari"
tell current tab of window 1
do JavaScript "document.getElementById('username').value = 'TheCoder';"
end tell
end tell

或者:

tell application "Safari"
do JavaScript "document.getElementById('username').value = 'TheCoder';" in document 1
end tell



<子>注意:示例AppleScriptcode就是这样,并且sans任何包含的错误处理不包含任何额外的错误处理可能是适当的。用户有责任根据需要添加任何错误处理。看看try语句错误AppleScript语言指南中的语句. 参见处理错误. 此外,延迟的使用在适当的情况下,在事件之间可能需要命令,例如delay 0.5,适当设置延迟

最新更新