简体   繁体   English

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

[英]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. 我看到了一些示例,这些示例解释了如何通过VBA在Internet Explorer中单击按钮。 However, the site that I need to use is not working. 但是,我需要使用的站点无法正常工作。 *It did not have an "id". *它没有“ id”。 I saw the function querySelector, but it did not work as well. 我看到了函数querySelector,但效果不佳。
Site: http://www2.bmf.com.br/pages/portal/bmfbovespa/boletim1/TxRef1.asp 网站: 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 的HTML是

<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

[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. 关于如何与“保存/打开”对话框进行交互, SO上有很多现有的答案 Personally, I prefer to automate with selenium basic and use Chrome to avoid this issue altogether 就个人而言,我更喜欢使用selenium basic自动化,并使用Chrome完全避免此问题

I am on a personal campaign to encourage people to use HTTP requests when it's possible, so here's my two cents: 我正在进行一项个人活动,以鼓励人们在可能的情况下使用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

Make sure you go to VB editor> Tools> References and add Microsoft WinHTTP Services version 5.1 and Microsoft HTML Object Library 确保转到VB编辑器>工具>引用并添加Microsoft WinHTTP Services version 5.1Microsoft HTML Object Library

With this method you don't need to download an excel file. 使用这种方法,您无需下载excel文件。 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. 学习代码,尝试学习它,我保证它会在以后的任何Web抓取项目中使您的生活更轻松。

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用VBA单击Internet Explorer中新打开的选项卡中的按钮? - How is it possible to click on a button present in a newly opened tab in internet explorer using VBA? Excel VBA 单击 Internet Explorer 中的按钮 - Excel VBA to click on a button in Internet Explorer 带有 getElementsByClassName 的 Internet Explorer 中的 Excel VBA 单击按钮 - Excel VBA click button in Internet Explorer with getElementsByClassName 双击按钮Internet Explorer Excel VBA - Double click button Internet Explorer Excel VBA 使用vba在Internet Explorer中单击链接文本 - click on link text in internet explorer using vba 使用VBA(getElementsByClassName)在Internet Explorer中单击一个按钮 - Clicking a button in Internet Explorer using VBA (getElementsByClassName) 如何在Internet Explorer中使用vba在弹出窗口中单击选项卡 - How do I click a tab inside a pop using vba in Internet Explorer 如何<a>通过 Excel VBA 在 Internet Explorer 中</a>单击超链接(<a>标记)?</a> - How to click the hyperlink (<a> tag) in Internet Explorer through Excel VBA? 如何使用VBA Excel Internet Explorer按下第二个按钮 - How to press the second button with VBA Excel Internet Explorer 当按钮没有关联的“名称”时,使用Excel VBA单击Internet Explorer中的按钮 - Use Excel VBA to click on a button in Internet Explorer, when the button has no “name” associated
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM