AppleScript中的JavaScript是否适用于Safari而不适用于Chrome

时间:2016-10-27 20:44:07

标签: javascript google-chrome safari applescript

我有一个带有嵌入式Javascript的AppleScript,可以在Safari上正常工作,但不适用于Chrome。

Safari版本(删除了非相关部分):

    tell application "Safari"
    activate
        do JavaScript "document.getElementsByName('files[]')[0].click();" in document 1 
    end tell

Chrome版本:

    tell application "Google Chrome"
    activate

    tell window 1 to tell tab 1

        execute  javascript "document.getElementsByName('files[]')[0].click();"

    end tell

Chrome版本返回"缺少值"在Apple ScriptEditor中。

我可以使用其他javascript命令工作,我可以

document.getElementsByName('files[]')[0].click();

document.getElementsByName("files[]")[0].click();"
在适当的网页上的Chrome控制台中的

(认为它可能与双引号有关 - 我猜想仍然可能)。

以下是有问题的元素:

<h3>Add new documents for this patient
         <!-- The fileinput-button span is used to style the file input field as button -->
        <span class="btn fileinput-button" style="float: right !important;">
            <span>Select Files to Upload</span>
            <input type="file" name="files[]" multiple="">
        </span>
    </h3>

有什么想法吗?我已经尝试了很多迭代。

document.getElementsByClassName('btn fileinput-button')[0].children[1].click();

在控制台中工作,但在AppleScript中则不行。看起来像AppleScript对我来说很奇怪,但是......

添加信息:

我可以点击&#34;点击&#34;在登录页面上,所以它不是一个网站范围的问题...

tell window 1 to tell tab 1

            execute javascript "document.getElementById('username').value='someLoser';"
            execute javascript "document.getElementById('password').value='YouWish'"
            execute javascript "document.getElementsByClassName('btn btn-md btn-primary')[0].click();"

        end tell

可以很好地嵌入AppleScript中。

感谢。

1 个答案:

答案 0 :(得分:0)

我认为这与阻止浏览器远程执行click事件的安全问题有关。在我设置的快速测试中,我能够从控制台执行jQuery和vanilla的单击,但我无法从AppleScript执行该操作。我没有错误,也没有任何错误。

我在其他问题中发现了多个类似的情景:

相关问题