VBA - 单击Morningstar.com上的JS按钮

时间:2015-05-19 11:02:15

标签: javascript excel vba excel-vba

在我搜索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

End Sub

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

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

  

< a class =" large_button" HREF ="的javascript:exportKeyStat2CSV();" >< span class =" csv" >< / span>< div> Export< / div>< / a>

感谢您的时间,期待听到有关如何解决这个问题的任何想法!

1 个答案:

答案 0 :(得分: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