需要脚本单击下载文件的网站上的按钮 [英] Need script to click button on website that downloads a file

查看:126
本文介绍了需要脚本单击下载文件的网站上的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此很陌生,所以这可能是一个愚蠢的问题,但是....

I am new to this so this is probably a dumb question but....

我正试图通过单击链接来从网站上进行下载,但我认为我的代码不适合AppleScript.

I am trying to get a download to happen off a website by clicking on a link but I don't think I have my code right for AppleScript.

该脚本打开了正确的网站,但是当我尝试通过单击导出数据"使它下载所需的文件时,下面的代码似乎没有执行任何操作,并且不确定我丢失了/错了什么.没有错误代码.只是什么都不做.

The script opens the right website, but when I try to get it to download the file I need by clicking "export data" the code below doesnt seem to do anything, and am not sure what I am missing/did wrong. No error code. Just doesnt do anything.

网站

感谢您的帮助.

推荐答案

以下示例 AppleScript 代码将打开一个新的 Safari 窗口到指定的 URL ,等待页面完成加载,然后点击导出数据 link 来下载 FanGraphs Leaderboard.csv 文件.

The following example AppleScript code will open a new Safari window to the designated URL, wait for the page to finish loading, then click the Export Data link to download the FanGraphs Leaderboard.csv file.

  • 注意::此测试已在 macOS High Sierra 上进行了测试,但是对于 macOS Mojave ,后来在waitForPageToFinishLoadingInSafari() handler 修改其 code .如果适用,请不要忘记这样做.

  • Note: This was tested on macOS High Sierra, however for macOS Mojave and later there is a note in the waitForPageToFinishLoadingInSafari() handler to modify its code. Don't forget to do it if applicable.

要将 JavaScript AppleScript Safari 一起使用,允许从Apple Events中使用JavaScript Safari > 开发菜单(默认情况下处于隐藏状态)必须选中.可以通过选中 [√]在菜单栏中显示开发"菜单来显示: Safari > 首选项... > 高级 >

To use JavaScript with AppleScript and Safari the Allow JavaScript from Apple Events on the Safari > Develop menu, which is hidden by default, must be checked. It can be shown by checking [√] Show Develop menu in menu bar in: Safari > Preferences… > Advanced

set theURL to "https://www.fangraphs.com/leaders.aspx?pos=all&stats=bat&lg=all&qual=50&type=c%2c6%2c11%2c12%2c13%2c21%2c23%2c39%2c35%2c34%2c41%2c42%2c43%2c104%2c107%2c110%2c206%2c209%2c211%2c50%2c61&season=2019&month=0&season1=2019&ind=0&team=0&rost=0&age=0&filter=&players=0"

tell application "Safari" to ¬
    make new document with properties {URL:theURL}

my waitForPageToFinishLoadingInSafari()

my clickId("LeaderBoard1_cmdCSV")


--  # Handlers:

to clickId(ElementID)
    tell application "Safari"
        do JavaScript "document.getElementById('" & ElementID & "').click();" in document 1
    end tell
end clickId

on waitForPageToFinishLoadingInSafari()
    --  # NOTE: For macOS Mojave and later, change 'UI element 1' to 'UI element 2` in the code below.  
    tell application "System Events"
        repeat until (accessibility description of ¬
            button 1 of UI element 1 of every group of toolbar 1 of window 1 of ¬
            process "Safari" whose name = "Reload this page") contains "Reload this page"
            delay 0.5
        end repeat
    end tell
end waitForPageToFinishLoadingInSafari


注意:示例 AppleScript 代码就是这样,并且不包含任何错误处理视情况而定.用户有责任添加任何适当,需要或想要的错误处理.看看 AppleScript语言指南 .另请参见 处理错误 .此外,使用


Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

这篇关于需要脚本单击下载文件的网站上的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆