javascript - 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 n
ordocument n
等。
示例 AppleScript 代码:
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
注意:示例 AppleScript 代码就是这样,并且没有任何包含的错误处理,不包含任何可能适当的额外错误处理。用户有责任根据需要或需要添加任何错误处理。查看AppleScript 语言指南中的try 语句和错误 语句。另请参阅处理错误。此外,在适当的情况下,可能需要在事件之间使用延迟命令,例如,使用延迟的值 delay 0.5
适当设置。