【问题标题】:How to click a button in Internet Explorer using VBA如何使用 VBA 在 Internet Explorer 中单击按钮
【发布时间】:2019-04-15 13:47:45
【问题描述】:

我看到了一些示例,这些示例解释了如何通过 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

【问题讨论】:

  • 上面提到的按钮在页面顶部和右侧。名称:“exportar para o excel”
  • @Vinicius,你的问题解决了吗?我测试了 QHarr 建议的解决方案,看起来他的代码运行良好,可以解决您的问题。我建议您测试他的代码并将他的建议标记为该线程的可接受答案。它可以在未来帮助其他社区成员解决类似的问题。如果您有任何其他问题,可以让我们知道。感谢您的理解。 +1 为 QHarr

标签: excel vba internet-explorer web-scraping


【解决方案1】:

你的选择器错误

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 来完全避免这个问题

【讨论】:

    【解决方案2】:

    我正在进行一项个人活动,以鼓励人们尽可能使用 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 文件。您可以从源头获取数据并写入工作表。

    研究代码,尝试从中学习,我保证它会让您在未来的任何网络抓取项目中生活更轻松。

    干杯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-27
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 2020-05-01
      相关资源
      最近更新 更多