结果“缺失值"使用 JavaScript document.getElementById 方法 [英] Result "Missing Value" with the JavaScript document.getElementById method

查看:18
本文介绍了结果“缺失值"使用 JavaScript document.getElementById 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自动进行搜索并专注于计算机在搜索栏中搜索的部分.我认为我的所有语法都是正确的,但是当我运行代码时,没有任何反应,并且在 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!")

除了你的 execute javascript ... code 行有点格式不正确,你可以比较你的和我的不同,你也没有告诉谷歌浏览器 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 ... command之后,你还必须等待execute的targetjavascript ...code 存在,因此使用 repeat until (loading of active tab of front window is false)>codeopen location ... command 之后,在 execute javascript ... 行之前.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆