如何使用VBA在Internet Explorer中单击按钮

时间:2019-04-12 14:59:51

标签: excel vba internet-explorer web-scraping

我看到了一些示例,这些示例说明了如何通过VBA在Internet Explorer中单击按钮。但是,我需要使用的站点无法正常工作。 *它没有“ id”。我看到了querySelector函数,但效果不佳。
网站:http://www2.bmf.com.br/pages/portal/bmfbovespa/boletim1/TxRef1.asp

Sub Download()

Dim user, password As Variant

Set IE = CreateObject("InternetExplorer.Application")
    IE.navigate "http://www2.bmf.com.br/pages/portal/bmfbovespa/boletim1/TxRef1.asp"
    IE.Visible = True

While IE.Busy
    DoEvents
Wend

    Application.Wait (Now + TimeValue("00:00:02"))
    'Preencher o Login e Senha
    IE.Document.querySelector("img[src='images/toolbar/b_edit.gif']").Click

End Sub

2 个答案:

答案 0 :(得分:2)

您的选择器错误

html是

<img style="CURSOR:HAND" src="http://www.bmf.com.br/bmfbovespa/images/comum/btoExcel.gif" align="absmiddle" hspace="0" onclick="salvaxls()">

您可以使用以下属性=值选择器

[onclick='salvaxls()']

您还可以在运算符中使用$结尾并指定src

[src$='btoExcel.gif']

使用适当的页面加载等待,如下所示

Option Explicit
'VBE > Tools > References:
' Microsoft Internet Controls
Public Sub RetrieveInfo()
    Dim ie As InternetExplorer
    Set ie = New InternetExplorer

    With ie
        .Visible = True
        .Navigate2 "http://www2.bmf.com.br/pages/portal/bmfbovespa/boletim1/TxRef1.asp"

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

        .document.querySelector("[src$='btoExcel.gif']").Click

        Stop

    End With
End Sub

关于如何与“保存/打开”对话框进行交互的existing answers on SO有很多。就个人而言,我更喜欢使用selenium basic自动化并使用Chrome完全避免此问题

答案 1 :(得分:0)

我正在开展一项个人运动,以鼓励人们在可能的情况下使用HTTP请求,所以这是我的两分钱:

Sub Taxas()
Dim req As New WinHttpRequest
Dim doc As New HTMLDocument
Dim table As HTMLTable
Dim tableRow As HTMLTableRow
Dim reqURL As String
Dim mainURL As String
Dim dateOfInterest As Date
Dim param1 As String
Dim param2 As String
Dim param3 As String
Dim i As Long
dateOfInterest = Date - 1 '11/04/2019 use whichever date you want
param1 = Format(dateOfInterest, "dd/mm/yyyy")
param2 = Format(dateOfInterest, "yyyymmdd")
param3 = "PRE" 'this can be changed according to which element from the drop down list on the top left you need
mainURL = "http://www2.bmf.com.br/pages/portal/bmfbovespa/boletim1/TxRef1.asp"
reqURL = mainURL & "?Data=" & param1 & "&Data1=" & param2 & "&slcTaxa=" & param3


With req
    .Open "POST", reqURL, False
    .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    .send
    doc.body.innerHTML = .responseText
End With
Set table = doc.getElementById("tb_principal1")
i = 1
For Each tableRow In table.Rows
    If tableRow.Cells(0).className <> "tabelaTitulo" And tableRow.Cells(0).className <> "tabelaItem" Then
        ThisWorkbook.Worksheets(1).Cells(i, "A") = CDbl(Replace((tableRow.Cells(0).innerText), ",", "."))
        ThisWorkbook.Worksheets(1).Cells(i, "B") = CDbl(Replace((tableRow.Cells(1).innerText), ",", "."))
        ThisWorkbook.Worksheets(1).Cells(i, "C") = CDbl(Replace((tableRow.Cells(2).innerText), ",", "."))
        i = i + 1
    End If
Next tableRow

End Sub

确保您进入VB编辑器>“工具”>“引用”并添加Microsoft WinHTTP Services version 5.1Microsoft HTML Object Library

使用此方法,您无需下载excel文件。您可以从源头直接获得数据,并将其写入工作表中。

研究代码,尝试向其学习,我保证它将在以后的任何Web抓取项目中使您的生活更轻松。

欢呼