简体   繁体   中英

How to click a button in Internet Explorer using VBA

I saw some examples that explain how to click at a button in Internet Explorer by VBA. However, the site that I need to use is not working. *It did not have an "id". I saw the function querySelector, but it did not work as well.
Site: 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

Your selector is wrong

The html is

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

You can use the following attribute = value selector

[onclick='salvaxls()']

You could also use $ ends with operator and target the src

[src$='btoExcel.gif']

Using a proper page load wait you have as follows

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

There are lots of existing answers on SO regarding how to interact with Save/Open dialog. Personally, I prefer to automate with selenium basic and use Chrome to avoid this issue altogether

I am on a personal campaign to encourage people to use HTTP requests when it's possible, so here's my two cents:

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

Make sure you go to VB editor> Tools> References and add Microsoft WinHTTP Services version 5.1 and Microsoft HTML Object Library

With this method you don't need to download an excel file. You get the data right from the source and write in your worksheet.

Study the code, try to learn from it and I promise it will make your life easier in any future web scraping projects.

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM