- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 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:frames
是 window
的属性,所以您必须使用 window.frames
(不是document.frames
)
4:document.getElementsById('apples')[0].click()
错了
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/
我是一名优秀的程序员,十分优秀!