无法使用VBA单击提交按钮

时间:2019-02-19 04:05:56

标签: javascript html vba web-scraping

当我使用(从日期)到(迄今为止)来获取数据时,请使用vba无法正常工作单击显示按钮

我还需要建议,如何从多个日期下载csv文件

我想为日期范围之间的每个日期下载csv文件

Dim MP, FD, TD As String
MP = ActiveWorkbook.Name
FD = Sheets("Sheet1").Range("XFA2")
TD = Sheets("Sheet1").Range("XFB2")
Dim IE As New SHDocVw.internetexplorer
Dim HTMLDoc As MSHTML.HTMLDocument
IE.Visible = True
IE.navigate "https://www.mcxindia.com/market-data/bhavcopy"
ShowWindow IE.hwnd, SW_SHOWMAXIMIZED
Do While IE.readyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = IE.document
For N = FD To TD
Workbooks(MP).Worksheets("Sheet1").Range("XEX2").Value = (FD)
N1 = Workbooks(MP).Worksheets("Sheet1").Range("XEX3").Value
HTMLDoc.getElementById("txtDate").Value = (N1)
HTMLDoc.getElementById("btnShowDatewise").Click
Next N

建议我单击“提交”按钮以及如何下载多个日期文件的代码,因为“显示”按钮需要花费一些时间来发布数据,所以我可以单击网页右侧的“ csv”标签

1 个答案:

答案 0 :(得分:1)

如果仅设置一天,则可以执行以下操作,其中包括检查日期是否生成下载文件

Option Explicit   
Public Sub Download()
    Dim ie As New InternetExplorer
    With ie
        .Visible = True
        .Navigate2 "https://www.mcxindia.com/market-data/bhavcopy"

        While .Busy Or .readyState < 4: DoEvents: Wend

        With .document
            .parentWindow.execScript "document.querySelector('#cph_InnerContainerRight_C001_txtDate_hid_val').value='20190201';"
            .parentWindow.execScript "document.querySelector('#txtDate').value='20190201';"
            .querySelector("#btnShowDatewise").Click

            If .querySelectorAll("#cph_InnerContainerRight_C001_lnkExpToCSV").Length > 0 Then
                .parentWindow.execScript "__doPostBack('ctl00$cph_InnerContainerRight$C001$lnkExpToCSV','');"
            End If

        End With

        While .Busy Or .readyState < 4: DoEvents: Wend
        '.Quit
    End With
End Sub