gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-27 23:38:24 26 4
gpt4 key购买 nike

我正在尝试在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com