![](/img/trans.png)
[英]How is it possible to click on a button present in a newly opened tab in internet explorer using VBA?
[英]How to click a button in Internet Explorer using VBA
我看到了一些示例,這些示例解釋了如何通過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
您的選擇器有誤
的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
關於如何與“保存/打開”對話框進行交互, SO上有很多現有的答案 。 就個人而言,我更喜歡使用selenium basic自動化,並使用Chrome完全避免此問題
我正在進行一項個人活動,以鼓勵人們在可能的情況下使用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.1
和Microsoft HTML Object Library
使用這種方法,您無需下載excel文件。 您可以直接從源中獲取數據並將其寫入工作表。
學習代碼,嘗試學習它,我保證它會在以后的任何Web抓取項目中使您的生活更輕松。
干杯
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.