I need a help, i'm trying to "select" the second checkbox (radio) in the website bellow with VBA but I cannot do this, can you help me?
1 - URL = https://ptax.bcb.gov.br/ptax_internet/consultaBoletim.do?method=gerarCSVTodasAsMoedas&id ||| 2 - Button is: Cotações de fechamento de todas as moedas em uma data. ||| 3 - Click on button "Pesquisar" ||| 4 - Download CSV (image bellow) to open those links bellow. ||| 4.1 If we can automate this "click" we dont need to look about the date, the web site already do this ||| 5 - Also if we can create a loop in the VBA to always change the last number of this URL we can also download the workbook I need to download? ||| 6 - Need to save this document in this path: C:\Temp\BASES\TBEX-OB08\todasasmoedas.csv
LINKS WE CAN AUTOMATE THE LAS NUMBER: Because this link changes all day to a 1 number check bellow: 11/08/2021 https://ptax.bcb.gov.br/ptax_internet/consultaBoletim.do?method=gerarCSVTodasAsMoedas&id=61795 10/08/2021 https://ptax.bcb.gov.br/ptax_internet/consultaBoletim.do?method=gerarCSVTodasAsMoedas&id=61794
Sub Web_Taxes()
Dim IE As Object
Dim URL As String
Set IE = CreateObject("InternetExplorer.Application")
URL = "https://www.bcb.gov.br/estabilidadefinanceira/historicocotacoes"
IE.Navigate URL
'Do While IE.busy Or IE.ReadyState <> 4
'Loop
'Need to select the button bellow in the website, it is the second "radio" button in the website.
'<input name="RadOpcao" id="RadOpcao" onclick="Opcao2()" type="radio" value="2">
IE.document.All.Item("RadOpcao").Item(2).Checked = True
End Sub
This the way I need to search (second button selected)
Also if you know other method to web scrape all the currencies information's by this website please let me know,
Thanks""
CodePudding user response:
After some simple test, I found that the radio button is located in the iframe
element, and the content of the iframe
element (ptax.bcb.gov.br) does not belong to the same domain as the parent page (www.bcb.gov.br), so I think it is impossible to get the radio element correctly to achieve automation.
If you need to specify the download file to a specific path, you can also try to use VBA to modify the data in the relevant registry path: HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer
,find Change the value of the string Download Directory
to the new path you want to assign as IE's Default Download directory.
Or you could change it manually, like this: open IE browser -> setting -> view downloads -> click options in popup -> Browser -> Select specific file path you need.
CodePudding user response:
I would use this:
Dim IE As InternetExplorer
Dim doc As MSHTML.HTMLDocument 'as HTML Document Object
Set IE = New InternetExplorer
IE.Visible = True ' Set to true to watch what's happening
IE.navigate "https://www.bcb.gov.br/estabilidadefinanceira/historicocotacoes"
Set doc = IE.document
doc.getElementsByName("RadOpcao")(2).checked = True
(2) is for the third item (staring with 0) unter the Name"Rad0pcao"
---- maybe it works... for you
If you want to click a button do this:
doc.getElementById("ID").Click
or this
doc.getElementsByClassName("CLASSNAME")(0).Click
If you want to select something try
doc.getElementsByClassName("CLASSNAME")(0).Value = "Value-Name"
When you use getElementsByClassName
, note that this is always an Array, so you have this (0)
after it, for the first Item in this Array, or you use (1)
for the second item... and so on.
Maybe it helps you, have fun :D