结果“缺少值"使用JavaScript document.getElementById方法 [英] Result "Missing Value" with the JavaScript document.getElementById method
问题描述
我正在尝试自动进行搜索,并重点关注计算机在搜索栏中键入搜索内容的部分.我认为我所有的语法都是正确的,但是当我运行代码时,什么也没发生,并且在applescript结果日志中,它显示缺少值".
I am trying to automate a search and a focusing on the part where the computer types in the search in the search bar. I think all of my syntax is correct but when I run the code, nothing happens and in the applescript result log it says "missing value"..
这是我的代码:
to inputByID(theId, theValue) --defines the function--
tell application "Google Chrome"
open location "https://www.google.com "
execute javascript " document.getElementById(' " & theId & " ').value = ' " & theValue
end tell
end inputByID
inputByID("lst-ib", "hi ")
我的语法错误还是我遗漏了一些东西?任何帮助表示赞赏..
Is my syntax wrong or am I missing something? Any help appreciated..
推荐答案
这在macOS High Sierra中对我有效:
This works for me in macOS High Sierra:
to inputByID(theId, theValue)
tell application "Google Chrome"
open location "https://www.google.com"
repeat until (loading of active tab of front window is false)
delay 0.1
end repeat
tell active tab of front window
execute javascript "document.getElementById('" & theId & "').value = '" & theValue & "';"
end tell
end tell
end inputByID
inputByID("lst-ib", "Hello World!")
除了您的 code 的execute javascript ...
行格式不正确的事实之外,您可以将自己的代码与我的代码进行比较以了解差异,还没有告诉Google Chrome Chrome execute javascript ...
行在哪里 code 的代码将被执行.因此,例如:tell active tab of front window
Aside from the fact that your execute javascript ...
line of code was a bit malformed, you can compare your's to mine for the difference, you also were not telling Google Chrome where the execute javascript ...
line of code was to be executed. Hence, e.g.: tell active tab of front window
此外,在使用open location ...
命令之后,您还必须等待 code 的execute javascript ...
行的 target 存在,因此在open location ...
命令之后和 code 的execute javascript ...
行之前使用 code 的repeat until (loading of active tab of front window is false)
行>.
Additionally, after using an open location ...
command, you also have to wait for the target of the execute javascript ...
line of code to exist, hence the use of the repeat until (loading of active tab of front window is false)
line of code after the open location ...
command, and before the execute javascript ...
line of code.
这篇关于结果“缺少值"使用JavaScript document.getElementById方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!