如何运行Applescript,它将单击并打开网页上的所有链接?

问题描述 投票:-1回答:1

我正在尝试编写一个脚本,该脚本将自动打开网页[http://www.legislation.gov.uk/new/uksi],然后单击“所有新立法”表中的所有链接。

到目前为止,我已经设法打开它,但是单击还是没有运气。

到目前为止是我的脚本:激活应用程序“ Safari”打开位置“ http://www.legislation.gov.uk/new/uksi

要clickID()

    do JavaScript "document.getElementById(id=per).click();" in document 1


end tell
hyperlink automation click applescript
1个回答
0
投票

以下示例 AppleScript 代码将在新的Safari 窗口中打开targetURL,等待页面完成加载,在目标页面上检索所有URLs,在它们中搜索指向今天发布的各种法定文书URLs,然后在新的[[tab中打开每个相同的[[window的targetURL已打开。

set targetURL to "http://www.legislation.gov.uk/new/uksi" set theseURLs to {} set grepSearchPattern to ".*\\.uk/uksi/.*\\|.*\\.uk/ssi/.*\\|.*\\.uk/wsi/.*\\|.*\\.uk/nisi/.*" tell application "Safari" make new document with properties {URL:targetURL} activate end tell tell application "System Events" repeat until exists ¬ (buttons of UI elements of groups of toolbar 1 of window 1 of ¬ process "Safari" whose name = "Reload this page") delay 1 end repeat end tell tell application "Safari" set allURLs to (do JavaScript "var a = document.links; var x = ''; var i; for (i = 0; i < a.length; i++) { x = x + a[i].href + '|'; }; " in document 1) end tell try set theseURLs to paragraphs of (do shell script "tr '|' '\\12' <<< " & allURLs's quoted form & " | grep " & grepSearchPattern's quoted form) end try if (length of theseURLs) is greater than 0 then tell application "Safari" to tell front window repeat with thisURL in theseURLs set current tab to (make new tab with properties {URL:thisURL}) end repeat set current tab to first tab end tell else display dialog " Nothing published on this date." buttons {"OK"} ¬ default button 1 with title "All New Legislation" with icon note end if 提示
提示:将鼠标悬停在水平和垂直滚动上可查看完整代码。

注意:

do JavaScript

    命令

targetURL页面上创建所有
  • URL
  • 管道分隔的字符串。do shell script 命令接受所有
  • URLs
  • 中以pipe分隔的string,并使用[[ C0],因此tr可以返回与grep匹配的URLsgrepSearchPattern 变量当前仅搜索法定文书,因为我认为这将显示在[[页面的
  • 所有新立法
  • 下的所有内容。由于grepSearchPattern中的targetURL,以及我在您发布问题以来在该URL处观察到的内容,C0]打开。如果您还希望links用于其他类型的立法,则可以调整/new/uksi variable以适应。注意:示例 AppleScript
    代码
    仅此而已,在不包含任何

    错误处理

    的情况下,不包含任何其他的错误处理
    ) 。用户有责任添加任何适当的,需要的或想要的。看一下targetURL中的grepSearchPattern statementtry statement。另请参见error
    © www.soinside.com 2019 - 2024. All rights reserved.