I need to access the value of an input and click on another with an Excel macro and I need to select the inputs through their class, since they do not have id. I have seen googling that there is a .getElementByClassName() method but there is no way to make it work. Not even accessing the supposed first element with "(0)"
IE.document.getElementByClassName("tarifas-index")(0)
I have also seen people doing a foreach selecting by tagName and going through the collection of selected elements to check with an If if .className == the one we want. Something more or less like this:
Dim IE As Object
Set IE = CreateObject("InternetExplorer.application")
IE.Visible = True
IE.Navigate "http://www.salvadorescoda.com/tarifas/index.htm"
Do
DoEvents
Loop Until IE.readystate = 4
'------------------------
'La siguiente linea es la que causa el error
For Each itemEle In IE.document.getElementByTagName("input")
If itemEle.className = "campocodigo" Then
itemEle.Value = "prueba"
End If
Next
Range("A2").Value = valor
But this code doesn't work for me, specifically the line after the comment. What am I doing wrong or not taking into account? What solution is there? I have never programmed with VBA and I confess that after programming JS and some PHP, this language makes me nervous. Thanks in advance!
Hello!
Your code was on the right track, and so was your idea of messing with the classname . The problem is the source code of the provided URL. If you read the source code, you will see that the element you are looking for is not actually in the HTML itself (I read it with Mozilla Firefox, and it doesn't work), because the input you are looking for is actually inside a frame that is load on the main. That is to say, it is in another document (if you know JS and PHP I am convinced that you know more than me about documents, frames and others, but I hope I have explained myself, I have no idea about PHP and JS, pure VBA is my thing) .
Knowing this, before trying to get the element, you have to access the frame document. Specifically, the frame you are interested in is called mainFrame .
My code is a copy of yours, but expanding a bit. To summarize, the steps are:
And now the code. I hope I can serve. Modify it for your needs.
The code:
What I get by running the code:
I hope it works for you, and I encourage you to continue with VBA, which is a very bloody language, but in the long run I think it's worth it.
All the best.
UPDATE: OP has a problem with IE 11, because it doesn't recognize the frames. Let's try another way to call them, see if it works.