【问题标题】:Is it possible to download a CSV file by using an Excel macro to click on a download button?是否可以通过使用 Excel 宏单击下载按钮来下载 CSV 文件?
【发布时间】:2020-01-25 02:34:54
【问题描述】:

我想在 Excel 中编写一个宏,该宏将从我用于工作的 Web 应用程序中下载 CSV。 Web 应用程序的用户界面让您单击按钮打开菜单,然后单击弹出菜单中的下载按钮。

我编写了一个应该打开 Internet Explorer 窗口的宏,然后单击下载按钮以下载我要下载的 CSV 文件。我还不能让它工作:它打开浏览器到我想要的网页,但它没有下载 CSV。

我通过使用“检查元素”获得了以下 HTML(我剪掉了看起来不相关的部分)。注意最后的下载按钮。

    <body style="overflow: hidden; padding-right: 8px;">
            <div role="presentation" class="MuiPopover-root" ... left: 0px;">
                    <div class="MuiPaper-root MuiMenu-paper MuiPaper-elevation8 MuiPopover-paper
                            MuiPaper-rounded"… transform-origin: 0px 26px;">
                    <ul class="MuiList-root MuiMenu-list MuiList-padding" role="menu" tabindex="-1">
                    ...
                    <a download="FileName.csv" class="downloadLinkButton" target="_self" href="blob:https://exampleURL.com/abcdefg1234">
                            <li class="MuiButtonBase-root MuiListItem-root MuiMenuItem-root MuiMenuItem-gutters MuiListItem-gutters MuiListItem-button"
                             tabindex="-1" role="menuitem" aria-disabled="false">Export to CSV</li>
                    </a>

这是我写的代码,但它不起作用:

    Dim objIE As InternetExplorer 'special object variable representing the IE browser
    Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element

    'initiating a new instance of Internet Explorer and asigning it to objIE
    Set objIE = New InternetExplorerMedium

    'make IE browser visible (False would allow IE to run in the background)
    objIE.Visible = True

    'navigate IE to this web page
    objIE.navigate "exampleURL.com"


    Do While objIE.Busy = True Or objIE.ReadyState <> 4: DoEvents: Loop

    Set elements = objIE.Document.getElementsByTagName("a")

    For Each ele In elements
        If (ele.className = "downloadLinkButton") Then
            ele.Click
            Exit For
        End If
    Next ele

正如我所提到的,这个宏不会出错,它只是将浏览器打开到我想要的网页。

有人对我如何自动化下载有什么建议吗? 我不太熟悉 Blob URL 的工作原理,但我认为下载 URL 会发生变化。 (我在代码/HTML 中的 URL 显然不是真正的 URL)。

谢谢!

编辑:下面是按钮的 HTML,必须单击该按钮以展开包含“导出为 CSV”选项的菜单。重要的部分开始于&lt;button class="MuiButtonBase-root MuiIconButton-root"

<div class="MuiGrid-root MuiGrid-item MuiGrid-grid-xs-8">
        <div class="MuiGrid-root MuiGrid-container MuiGrid-direction-xs-column MuiGrid-align-items-xs-flex-end">
                <div class="icon-container">
                        <span class="lastupdated-container…</span>
                         …
                        <button class="MuiButtonBase-root MuiIconButton-root" tabindex="0" type="button" id="card-view-more" aria-label="More">
                                <span class="MuiIconButton-label">
                                        <span class="material-icons MuiIcon-root" aria-hidden="true">more_vert</span>
                                </span>
                        </button>
                </div>
        </div>
</div>

【问题讨论】:

  • ele.click 之后会发生什么?有下载提示吗?
  • 点击没有被触发。我认为.className = "downloadLinkButton" 找不到下载按钮。也许有更好的方法来找到下载按钮?

标签: html excel vba web-scraping


【解决方案1】:

首先:使用alwaysOption Explicit作为every 代码模块!

第二:你不需要循环来找到下载链接。您可以使用 Set elements = objIE.Document.getElementsByClassName("downloadLinkBut​​ton")(0) 直接获取链接,如果它是文档中与此 CSS 类的唯一链接。

第三:可能是页面需要更多时间才能完全加载,因为也有必须加载的信息。然后你需要休息一下。您可以使用 Application.Wait

来做到这一点

第四:如果显示的行产生错误,我认为你不处理弹出的html代码是链接。

试试这个:

Option Explicit

Sub DownloadCSV()

Dim objIE As InternetExplorer 'special object variable representing the IE browser
'Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
Dim elements As Object

  'initiating a new instance of Internet Explorer and asigning it to objIE
  Set objIE = New InternetExplorerMedium

  'make IE browser visible (False would allow IE to run in the background)
  objIE.Visible = True

  'navigate IE to this web page
  objIE.navigate "exampleURL.com"

  Do While objIE.Busy = True Or objIE.ReadyState <> 4: DoEvents: Loop
  'Break for 5 seconds to looad more data if needed
  Application.Wait (Now + TimeSerial(0, 0, 5))

  Set elements = objIE.Document.getElementsByClassName("downloadLinkButton")(0)

  'Check whether the variable elements contains a HTML element
  'with the CSS class "downloadLinkButton"
  If Not elements Is Nothing Then
    'Wanted element found
    elements.Click
  Else
    'Wanted element not found
    MsgBox "There is no Element with the CSS class 'downloadLinkButton' in the used HTML document"
  End If
End Sub

我不知道它在您的网站上是什么样的弹出窗口。但是在这里你可以看看如何通过代码生成页面、HTML 事件和(我认为你会需要它)SendKeys() 来解决这些问题: How to use excel vba to click on interactive dialog pop-up?

如果您获得下载链接,您还可以使用 API 函数 URLDownloadToFile() 代替 click 和 SendKeys()。 SendKeys() 在大多数情况下是一个非常糟糕的解决方案。

【讨论】:

  • 非常感谢您的回复!我使用了您的一些代码来确认 Excel 没有找到下载按钮元素(elements 仍然是 Nothing)。我尝试将 .getElementsByClassName() 与其他类名一起使用,但它找不到任何东西 - 是什么导致 Excel 无法通过类名找到任何元素?我想如果我可以从下载按钮元素中获取 href,那么我就可以使用该 URL 下载我想要的 CSV。
  • 在 HTML 中的 下,它有 &lt;noscript aria-hidden="true"&gt;You need to enable JavaScript to run this app.&lt;/noscript&gt;。有 3 个 &lt;script&gt; 元素...也许这些脚本需要运行才能访问下载按钮?如果是这样,我该如何解决?
  • 我觉得可以有两种弹窗。打开另一个浏览器窗口或更改页面代码以显示菜单。你必须对所使用的技术做出反应。但我不能为你做这件事,因为我不知道页面。使用了哪种技术?
  • 没有弹出窗口:当您单击“打开菜单”按钮时,菜单会展开。此菜单包含“导出为 CSV”选项。当点击“打开菜单”按钮时,页面的html发生变化:&lt;body&gt;style发生变化,出现&lt;div role = "presentation"... &lt;/div&gt;,这是之前没有显示的。这是包含下载按钮的&lt;div&gt;。 (下载按钮在&lt;ul&gt; 内,而在另一个&lt;div&gt; 内)。
  • 我仍然不确定您是否已经可以自动打开弹出窗口。我在宏下面的答案中的链接向您展示了它在需要由 HTML 事件触发时是如何工作的。但是你必须知道哪个事件。在浏览器中打开页面并按 F12。在 DOM 检查器中单击 HTML 源代码,直到到达必须为弹出窗口触发的按钮。在 DOM Inspector 中查看按钮代码末尾是否有一个标记为 Events 的小按钮。点击它并告诉我显示了哪些事件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-29
  • 2018-04-06
  • 1970-01-01
  • 2011-10-16
  • 2018-06-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多