1

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
2
  • The button that mentioned above is in the on the top of the page and on the right. Name: "exportar para o excel" Commented Apr 12, 2019 at 15:02
  • @Vinicius, Is your issue got solved? I test the solution suggested by QHarr and looks like his code is working fine and can solve your issue. I suggest you to test his code and mark his suggestion as an accepted answer for this thread. It can help other community members in future in similar kind of questions. If you have any further question than you can let us know about that. Thanks for your understanding. +1 for QHarr Commented Apr 15, 2019 at 2:03

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.