Applescripts getElementByName()。value not working

时间:2018-03-15 14:18:07

标签: applescript

我尝试使用AppleScript使用Excel中的数据填充Web表单,但Safari说:

TypeError: undefined is not an object (evaluating 'document.getElementsByName('txtnama')[0].value=txtnama')

以下是代码:

tell application "Microsoft Excel"
    activate

    set txtnama to value of cell "I2"

    tell application "Safari"
        activate

        do JavaScript "

        var txtnama= '" & txtnama & "'; 

        document.getElementsByName("txtnama")[0].value=txtnama;" in document 1

    end tell
end tell

我试图填写的表单元素是:

<input type="text" name="txtnama" style="width:300px;" maxlength="80" value="">

1 个答案:

答案 0 :(得分:0)

您的报价不匹配和/或未转义,但基本代码是正确的。以下是使用正确引用重新格式化代码的 Safari 部分:

    tell application "Safari"

        do JavaScript ¬
            "var txtnama=" & quoted form of txtnama & ";" & ¬
            "document.getElementsByName('txtnama')[0].value=" & ¬
            quoted form of txtnama ¬
            in document 1

    end tell

您也不应将 Safari tell块嵌套在 Microsoft Excel tell块中,除非有特定的内容这样做的理由。因此,在定义变量end tell之后,将txtnama移到最后。它应该像这样布置:

    tell application "Microsoft Excel"
        -- Commands sent to Excel
    end tell


    tell application "Safari"
        -- Commands sent to Safari (As above)
    end tell