Applescript,单击复选框并执行JavaScript

时间:2015-11-25 19:45:37

标签: javascript html web applescript

我试图在AppleScript中使用do JavaScript检查或取消选中复选框。该复选框显示在名为mainFrame的iframe中,位于名为postForm的表单中...此处是复选框:

<input class="normal" id="apples" name="applesChk" value="<1>" style="visibility: visible" onclick="checkChk(this.checked, 1);" type="checkbox">

这是我的AppleScript ......这会让我有点尴尬:

tell application "System Events"
    tell process "Safari"

        set windowNumber to 1
        repeat the number of windows times
            if "(item)" is in name of window windowNumber then
                set tempWindowName to name of window windowNumber
            else
                set windowNumber to windowNumber + 1
            end if
        end repeat

    end tell
end tell

tell application "Safari"
    do JavaScript "document.['apples'].click()" in window tempWindowName
    do JavaScript "document.getElementsById('apples')[0].click()" in window tempWindowName
    do JavaScript "document.forms['postForm']['apples'].checked = true" in window tempWindowName
    do JavaScript "document.frames['mainFrame']['apples'].checked = true" in window tempWindowName
    do JavaScript "document.frames['mainFrame'].forms['postForm']['apples'].checked = true" in window tempWindowName
end tell

我通过查找包含某个短语的窗口名称来获取窗口,然后我使用它。然后你可以看到我五次尝试复选框。我真的很难在这里成功地追随其他例子......任何建议都值得赞赏,我哪里出错了?提前谢谢。

1 个答案:

答案 0 :(得分:1)

1:使用do javascript命令时,您必须指定文档标签,如下所示:

do JavaScript "some command" in document 1 -- not -->window 1
do JavaScript "some command" in (current tab of window 1)

2:您可以按名称找到文档(或当前标签),如下所示:

tell application "Safari"
    set tDoc to first document whose name contains "(item)"
    do JavaScript "some command" in tDoc
    -- or 
    --set tTab to current tab of (first window whose name contains "(item)"
    --do JavaScript "some command" in tTab
end tell

3:frameswindow的属性,因此您必须使用window.frames(不是document.frames

4:document.getElementsById('apples')[0].click()错误

  1. getElementsById 不是有效命令,请使用 getElementById
  2. getElementById返回一个元素,而不是数组
  3. document.getElementById('apples').click()是正确的
  4. 如果您的信息正确无误,此脚本将有效(如果iframe不在另一个iframe中):

    tell application "Safari"
        set tDoc to first document whose name contains "(item)"
        do JavaScript "window.frames['mainFrame'].document.getElementById('apples').checked=true" in tDoc
    end tell
    

    此脚本已在包含iframe(<iframe name="mainFrame" ...)的HTML文档上进行了测试,此iframe包含表单(<form action="demo_form.asp" name="postForm" ...),此表单包含复选框(<input class="normal" id="apples" ... type="checkbox">