Problems with web automation in VBA - Radio Button

Asked

Viewed 55 times

1

Hi, I’m new around here, I am facing a problem with my VBA code, I am trying an interaction using the internet explore, I need you to do the following steps

  1. access the site: https://www.bcb.gov.br/estabilidadefinanceira/historicocotacoes
  2. select the radio button 2 <"Closing quotations of all currencies in a date.">
  3. click search.

however, when I try to teach the radio button, it appears an error. below is my code.

    Sub buscador()
    
    Set ie = CreateObject("InternetExplorer.application")
    
    ie.navigate "https://www.bcb.gov.br/estabilidadefinanceira/historicocotacoes"
    ie.Visible = True
    
    Do While ie.Busy And ie.readyState <> ""
    DoEvents
    Loop
    
    ie.document.getElementById("RadOpcao").Item(1).Click
    
    ie.document.getElementsByClassName("fundoPadraoBClaro3").Item(0).getElementsByTagName("input").Item(1).Click
    
    
    End Sub

HTML I’m taking the classes

Screenshot of the html code

  • getElementById as it says the method name picks up an element, so you don’t need the .Item pq will not return an array but a single object

  • @Ricardopunctual I tested with the following expression ie.document.getelementsbytagname("Radopcao")(0). Click however failed, gave error 91

  • I believe you don’t know much about html, I think you need to take a look at :) getElementsByTagName will look for a tag as the method name says, I do not know a "Radopcao" tag in html :) this method is used for "input", "button", "div", etc

1 answer

0


The URL you are trying to interpret through Internetexplorer.application has an iframe (basically a page inside another page, see more here).

Iframes limit selections and executions, including routines defined by VBA.

The solution is to search for access directly on this page that is embedded in the main page. By inspecting the page you can get to it (URL base), see:

inserir a descrição da imagem aqui

Now we can adjust the VBA code with this URL. Still, I changed the search lines and click, using querySelector from value, which apparently is a unique value in each element (the ID of the HTML elements are repeated, which is not recommended, and may generate some error):

Sub buscador()

Set ie = CreateObject("InternetExplorer.application")

ie.navigate "https://ptax.bcb.gov.br/ptax_internet/consultaBoletim.do?method=exibeFormularioConsultaBoletim"
ie.Visible = True

Do While ie.Busy And ie.readyState <> ""
DoEvents
Loop

ie.document.querySelector("input[value='2']").Click
ie.document.querySelector("input[value='Pesquisar']").Click

End Sub
  • perfect friend, it worked!!

  • now I’m in the "download" part of the file. I’ll try here. Thank you very much.

  • Great. Signals as an appropriate response, can help more people. If new problems arise, create a new question to not mix up the themes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.