需要脚本来单击下载文件的网站上的按钮 [英] Need script to click button on website that downloads a file
问题描述
我是新手,所以这可能是一个愚蠢的问题,但是....
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.
网站 这里
to clickId(LeaderBoard1_cmdCSV)
tell application "Safari"
do JavaScript "document.getElementById('" & LeaderBoard1_cmdCSV & "').click();" in document 1
end tell
end clickId
感谢您的帮助.
推荐答案
以下示例 AppleScript 代码将打开一个新的Safari 窗口到指定的URL,等待页面加载完成,然后点击导出数据strong> 链接以下载 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 和更高版本的 code>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 > Develop 菜单,默认隐藏,必须勾选.可以通过勾选[√] Show Develop menu in menu bar来显示:Safari > Preferences... > Advanced>
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语言指南.另见,处理错误.此外,使用 delay command 在适当的事件之间可能是必要的,例如delay 0.5
,适当设置delay的value.
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屋!