How to click on a web button from excel that has neither id nor name?

1

I need in Excel (vba) to make a macro or robot to enter some data in a web page taking as values a list of data (cedula and date) that I have in an Excel sheet, those parameters I can already pass to the form with this element "IE.Document.getElementById", but when I try to click the button that appears to be able to complete the operation I want, I see that the button as such does not have a variable id or the name to be able to click from Excel.

I leave the webpage I'm trying to access:

link

I still get an error, I opened a new module in Excel vba and run the program but it gives me a syntax error in the line that I highlight with asterisks:

Sub CARGAR_DATOS_WEB()
 Dim IE As Object
 Dim allelements As Object


 Application.ScreenUpdating = False
'Creamos objeto internet explorer
 Set IE = CreateObject("InternetExplorer.Application")
 'abrimos web
 IE.navigate "https://aplicaciones.msp.gob.ec/coresalud/app.php/publico/rpis/afiliacion/consulta"
'esperamos a que se carguen todos los elementos
 Do Until IE.ReadyState = 4
 DoEvents
 Loop
'si necesitamos más tiempo lo podemos configurar aquí
 Application.Wait (Now + TimeValue("0:00:1000"))
'localizamos el ID que hace referencia al cuadro de búsqueda
 'esto lo hacemos buscando en el código HTML de la página web
 'e igualamos el valor de la celda para realizar la búsqueda

 IE.document.getElementById("identificacion").Value = "0960413078"

 IE.document.getElementById("fechaconsulta").Value = "10-06-2017"


'también buscamos ID correspondiente al botón para buscar el valor

***IE.document.getElementsByClassName("btn-primary")[0].click();***


'hacemos visible la web.
 IE.Visible = True
 Set IE = Nothing
 Application.ScreenUpdating = True
 End Sub
    
asked by Marlon 14.06.2017 в 23:23
source

4 answers

-1

is my first answer in Spanish, please check next time the object to which you are going to refer, since sometimes there are tags between the code that can change the indexation of an object and that are not visible confuse us a little bit, in your specific case it changes

this line

***IE.document.getElementsByClassName("btn-primary")[0].click();***

for this

 IE.document.getelementsbytagname("button")(1).Click

I recommend the next time to perform an iteration with a for loop for all the elements you search on the page, either tags or objects as such so that you can search for an attribute that makes them all different and you can manipulate the object with a single line of code

I forgot to change

Application.Wait (Now + TimeValue("0:00:1000"))

for this

Application.Wait (Now + TimeValue("0:00:01"))

parameters: TimeValue ( hours: minutes: seconds )

    
answered by 21.06.2017 / 00:45
source
1

Hi Marlon, the correct way in VBA is this:

document.getElementsByClassName("btn btn-primary")(0).Click

I hope it serves you.

    
answered by 07.07.2017 в 22:22
0

The code has two errors

Instead of

IE.document.getElementsByClassName("btn-primary")(0).click

must be

IE.document.getElementsByClassName("btn-primary")(0).Click
  

Note the use of initial capitalization in Click .

and instead of

Application.Wait (Now + TimeValue("0:00:1000"))

must be

Application.Wait (Now + TimeValue("0:00:10"))

By the way, I tried it in Excel 2016.

    
answered by 07.07.2017 в 23:12
-2

The button shown on that page has the "btn-primary" class of bootstrap

document.getElementsByClassName("btn-primary")[0].click();

As a tip I recommend you put it in a setTimeout with about 1000 milliseconds so you can time the page to load.

    
answered by 14.06.2017 в 23:38