使用 AppleScript 和 JavaScript 填充输入字段

populate input field using AppleScript and JavaScript

我已经搜索过堆栈溢出,但提供的解决方案没有用,所以我发布了这个问题:

我有以下 HTML 元素:

<input xmlns="http://www.w3.org/1999/xhtml" id="loginform:fPers" type="text" name="loginform:fPers" value class="inputFieldApp" maxlength="20" tabindex="4" title="Some title"> 

这是一个来自网站的现有 HTML 元素,我想使用 JavaScript 进行填充。

我正在使用 AppleScript 编写脚本。 该脚本只是打开 Chrome 但什么都不做: 它应该单击一个按钮并填充一个输入字段。

tell application "Google Chrome"
    activate

    set thescript to "document.getElementById(\"loginform:kommenbtn\").click();"

    set thescript2 to "document.getElementById(\"loginform:fPers\").value = \"112233\";"
    execute javascript thescript2
end tell

我在 www.google.de 上试过这个,看看问题是否是特定于站点的,但它也不起作用。我想获取该网站上的国家/地区数据。它具有以下 class:

<span class="_Vbu">​Deutschland​</span>​

我试着用class名字来称呼它:

tell application "Google Chrome"
    activate
    set thescript to "document.getElementByClassName('_Vbu').innerHTML"
    set myvar to execute active tab of front window javascript thescript
    display dialog myvar
end tell

我总是得到以下显示对话框内容:msng

如评论中所述,[0] 成功了。

类名是正确的,如下面的截图所示:

这是使用 Google Chrome:

的工作原理
tell application "Google Chrome"
    activate
    set thescript to "document.getElementsByClassName('_Vbu')[0].innerHTML;"
    set myvar to execute active tab of front window javascript thescript
    display dialog myvar
end tell

这是使用 Safari 时的工作方式:

tell application "Safari"
    set thescript to "document.getElementsByClassName('_Vbu')[0].innerHTML;"
    tell document 1
        set myvar to do JavaScript thescript
    end tell
    display dialog myvar
end tell

如果您收到错误 "myvar is not declared",请确保您在 AppleScript 中找到了正确的文档。

它两次都在显示对话框 ("Deutschland") 中为我提供了正确的文本。