javascript - Applescript,单击带有 do JavaScript 的复选框

标签 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:使用 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()是正确的
<小时/>

如果您的信息正确,则此脚本将起作用(如果 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"> )。

关于javascript - Applescript,单击带有 do JavaScript 的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33925183/

相关文章:

javascript - 通过 JavaScript 检查 php session ,如果不存在则不会导致任何错误

html - 在 RStudio 中,knitr markdown 输出在哪里?

redirect - 如何在 Google Cloud 中将 www 重定向到非 www?

javascript - 更改 DIV 的内容

javascript - 迭代 typescript map 失败

javascript - 在 javascript 中使用 blob 写入时文件开头出现意外字符

javascript - 在 div 的图像 slider 中将每个图像居中

javascript - 如何在鼠标不动时使功能起作用?

javascript - 使用增量改进客户端-服务器数据同步功能

javascript - 随时随地设置文本区域中的文本颜色