Applescript:用JS点击按钮

Applescript : click on button with JS

我正在尝试使用 Applescript 在 Safari 上自动点击按钮下载文件

到目前为止,我已经尝试了几种方法,但都没有用。最好的情况是,它 returns 什么都没有,只是转到脚本的下一部分。

每个请求的下载文件都会发生变化。

这是我使用的网页上显示的 class:

<div id="events-table"> = [=10=]
  <div class="bootstrap-table">
    <div class="fixed-table-toolbar">
      <div class="bs-bars pull-right">
        <div id="table-toolbar" class="ml-3">
          <a class="btn btn-default" href="/event_sequences/myfile.csv">Download CSV</a>
        </div>
       </div>
      <div class="columns column-right btn-group pull-right>...</div>
      ::after
    </div>
    <div class="fixed-table-container" style="padding-bottom: 0px;">...</div>
   </div>
   <div class="clear fix">...</div>
  </div>
</div>

下面是我试过的一些代码,none 成功了


tell application "Safari"
do JavaScript "document.title = 'Download';" in current tab of window 1
end tell

tell application "Safari"
do JavaScript "document.title = 'Download CSV’;” in current tab of window 1
end tell

tell application "Safari" to ¬
do JavaScript ¬
"document.querySelectorAll(\"[href=\\"Download CSV\\"]\")[0].click();" in current tab of window 1


我也试过 "manually" 但它也没有用,因为看起来我无法弄清楚如何到达按钮:

Tell application "System Events" to tell process "Safari"
    set frontmost to true

    repeat 7 times
        keystroke option
        keystroke (ASCII character 9)

    end repeat
    keystroke return
end tell

当我尝试手动访问页面上的按钮时,7 Option Tab 正是我需要访问的,但如果我使用脚本,它会在下方并替换按钮下方字段中的一些值。我尝试增加和减少重复次数,但没有成功。

提前感谢您的帮助!

如果页面上只有那个 class 名称的下载按钮,请尝试以下操作:

tell application "Safari" to do JavaScript "document.getElementsByClassName('btn btn-default')[0].click();" in document 1