【问题标题】:VBA - Click a JS button on Morningstar.comVBA - 单击 Morningstar.com 上的 JS 按钮
【发布时间】:2015-05-19 19:30:09
【问题描述】:

在我对 stackoverflow 的搜索过程中,我发现了许多与此类似的主题,但没有可靠的答案。我要做的是转到下面的网站并单击“导出 CSV”JS 按钮。

到目前为止,我从另一个 VBA 编码器“复制和借用”的是:

Sub GetDat()

Set IE = CreateObject("InternetExplorer.Application")

With IE
    .Visible = True
    .navigate "http://financials.morningstar.com/ratios/r.html?t=JNJ&region=USA&culture=en_US"
    .Top = 50
    .Left = 530
    .Height = 600
    .Width = 1000

Do Until Not IE.Busy And IE.readyState = 4
    DoEvents
Loop

End With

IE.Document.getelementsbytagname("Export").Item(1).Click

结束子

我已尝试将其延迟 5-10 秒以等待页面加载。有人提到 getelementsbytagname 没有找到按钮...?

另外,我检查了按钮的元素。按钮的 html 是(我添加了空格来显示整个代码):

导出

感谢您抽出宝贵时间,期待听到有关如何解决此问题的任何想法!

标签: javascript vba excel


【解决方案1】:

您不应该使用浏览器来简单地下载文件,使用 URLDownloadToFileA API:

Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, _
    ByVal szURL As String, ByVal szFileName As String, _
    ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long

Sub Download()
    URLDownloadToFile 0, "http://financials.morningstar.com/ajax/exportKR2CSV.html?&callback=?&t=XNYS:JNJ&region=usa&culture=en-US&cur=&order=asc", _
                         "C:\Temp\Export.csv", 0, 0
End Sub

【讨论】:

  • 感谢您的回复凯尔。这完全超出了我的技能范围,所以我将不得不研究它。我收到一个编译错误:End Sub...等之后只能出现 cmets。我会做更多的研究!再次感谢!
  • 原样放入空白模块即可。 API 声明必须在顶部
猜你喜欢
  • 1970-01-01
  • 2019-05-25
  • 2019-01-11
  • 1970-01-01
  • 1970-01-01
  • 2021-12-16
  • 2013-10-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多