Hyperlink 如何运行Applescript以单击并打开网页上的所有链接?

Hyperlink 如何运行Applescript以单击并打开网页上的所有链接?,hyperlink,automation,click,applescript,Hyperlink,Automation,Click,Applescript,我正在尝试编写一个脚本,它将自动打开一个网页,然后单击“所有新立法”表中的所有链接 到目前为止,我已经设法让它打开页面,但没有点击运气 以下是我目前的脚本: activate application "Safari" open location "http://www.legislation.gov.uk/new/uksi" to clickID() do JavaScript "document.getElementById(id=per).click();" in document

我正在尝试编写一个脚本,它将自动打开一个网页,然后单击“所有新立法”表中的所有链接

到目前为止,我已经设法让它打开页面,但没有点击运气

以下是我目前的脚本:

activate application "Safari"
open location "http://www.legislation.gov.uk/new/uksi"

to clickID()
    do JavaScript "document.getElementById(id=per).click();" in document 1
end tell
以下示例AppleScript代码将在新的Safari窗口中打开
targetURL
,等待页面完成加载,在目标页面上检索所有URL,搜索指向今天发布的各种法定文书的URL,然后在打开
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/.*"
set jsStatements to "var a = document.links; var x = ''; var i; for (i = 0; i < a.length; i++) { x = x + a[i].href + '|'; };"

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 jsStatements 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
set targetURL to "http://www.legislation.gov.uk/new/uksi"
set theseURLs to {}
set lynxCommand to "/usr/local/bin/lynx --dump -listonly -nonumbers -hiddenlinks=ignore"
set grepSearchPattern to ".*\\.uk/uksi/.*\\|.*\\.uk/ssi/.*\\|.*\\.uk/wsi/.*\\|.*\\.uk/nisi/.*"

try
    set theseURLs to paragraphs of ¬
        (do shell script lynxCommand & space & targetURL's quoted form & ¬
            " | grep " & grepSearchPattern's quoted form)
end try

if (length of theseURLs) is greater than 0 then
    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" 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
提示:鼠标悬停并水平/垂直滚动以查看完整代码

注意事项:

  • lynxCommand
    变量中,将
    /usr/local/bin/lynx
    更改为相应的
    /path/to/lynx
    lynx
    可以使用自制软件安装
  • grepSearchPattern
    变量当前仅搜索法定文书,因为我假设这是
    targetURL
    打开页面上所有新立法下显示的全部内容,因为
    targetURL
    中的
    /New/uksi
    ,自从你发布这个问题以来,我在URL上观察到了什么。如果还需要其他类型立法的链接,可以调整
    grepSearchPattern
    变量以适应


注意:示例AppleScript代码就是这样的,任何包含的错误处理都不包含任何其他适当的错误处理。用户有责任根据需要添加任何适当的错误处理。请查看中的语句和语句。另请参见,.

您的单击指令只需单击id=per,它就是页面的主体。所以什么也没发生。我想你也应该点击特定的按钮,这是你必须通过do java脚本指令点击的元素。你说,“……然后点击该页面上包含的所有链接”。你的意思是“该页面上包含的所有链接”,包括主页、了解立法、欧盟立法和英国法律,浏览法规、法规变更、搜索法规、高级搜索、今天、1月6日、12月27日、12月24日、12月23日、12月20日、12月19日、12月18日、12月17日、12月16日、帮助、关于我们、网站地图、可访问性、联系我们、隐私通知、Cookies等?我看到您在我之前的评论之后更新了问题,包括然后单击“所有新立法”表中的所有链接。“,但我怀疑你不想点击所有新立法下的所有链接,还是?您是否真的想要英国、苏格兰、威尔士、北爱尔兰的链接,如果存在英国法定文书、苏格兰法定文书、威尔士法定文书、北爱尔兰法定文书,还是只想要实际新立法的链接?你真的需要明确和具体地说明你想要什么!这就是说,我认为你应该忘记点击实际新立法的链接,而是专注于在新标签中打开目标链接的URL列表。我认为这是实现目标的更好/更容易的方法。假设只是实际新立法的链接,我注意到这些URL上有一种模式,可以从页面上所有URL的列表中轻松搜索它们。我已经用这种方法发布了一个答案。