【发布时间】: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”选项的菜单。重要的部分开始于<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