在网络表单中输入值并使用 applescript 按 return 键

Enter value in a webform and push the return key with applescript

我想在网络表单上输入一个值,然后按回车键,因为页面上没有可供点击的按钮

我无法提供任何 URL,因为它是一个私人网络工具。

搜索框是一个简单的网络表单,我可以在其中输入一个值,然后按回车键以对结果收费。

我已经编写了在框中添加值的脚本部分,没问题。我的问题是找到一种方法让击键 return 在页面上工作。

set theValue to "1111111"

to inputByID(theId, theValue)

    tell application "Safari"

        do JavaScript "document.getElementById('" & theId & "').value = '" & theValue & "'" in document 1

    end tell

end inputByID

inputByID("TextField", theValue)

所以这很完美。然后我尝试在每个我能想到的地方添加一个 Keystroke return,但我总是收到相同的错误消息:

Result:
error "Safari got an error: Can’t get keystroke \"
\"." number -1728 from keystroke "
"

我不确定这里是否还有其他事情要做。看Page Source找不到相关资料,所以很想走这条路。

要在您的场景中使用 击键,必须满足两个条件。

  • Safari 需要处于最前面且处于活动状态。
  • 光标必须位于可以接收输入的文本字段中。

AppleScript代码为:

tell application "Safari" to activate
delay 0.5
tell application "System Events" to keystroke return

使用系统事件,对于return,您还可以使用:

tell application "System Events" to key code 36

请注意,delay 命令 允许应用程序在发送 击键 之前处于最前面并处于活动状态。需要调整 as/if。