I am trying to access a web page with a macro in Excel, enter a code in an input, press a button and copy the price resulting from the search. It's this last step that I can't. The element that contains the price has the id #result_ok but one of the drawbacks is that it is a text node that has not been inside anything and at the same time has three siblings. I show you:
I've tried to do a getElementById and access its innerText or innerHTML and nothing... You get code that doesn't correspond to your inner text or various errors when trying other things.
Sub buscar()
Dim IE As InternetExplorer
Dim DOC As HTMLDocument
Dim url As String
Set IE = Nothing
Set DOC = Nothing
url = "http://www.salvadorescoda.com/tarifas/index.htm"
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate url
While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE
DoEvents
Wend
Set DOC = IE.document.frames
Dim iFrameDoc As HTMLDocument
Set iFrameDoc = DOC.Item("mainFrame").document 'especificamos el nombre del frame
If iFrameDoc Is Nothing Then
MsgBox "No existe ningún frame con el nombre especificado."
IE.Quit
Set IE = Nothing
Exit Sub
End If
Dim itemEle As Object
'ya estamos en el frame, ahora podemos conseguir el elemento INPUT
For Each itemEle In iFrameDoc.getElementsByTagName("input")
If itemEle.getAttribute("class") = "campocodigo" Then
itemEle.Value = ActiveCell.Value
Exit For
End If
Next
'buscamos y clickamos el botón buscar
For Each itemEle In iFrameDoc.getElementsByTagName("input")
If itemEle.getAttribute("class") = "floatright botonbuscar" Then
itemEle.Click
Exit For
End If
Next
'¿Como obtengo el texto?
ActiveCell.Offset(0, 1).Value = iFrameDoc.getElementsById("result_ok").innerHTML
'IE.Quit
'Set IE = Nothing
End Sub
Do you know what the problem is? How can I get it? Thank you very much in advance!
Hello!
It's the same as always. It's about making a loop with all the DIV elements you have, until you identify the one that interests you.
I have added to the code, after the part where you click search, the following:
To summarize the process, what I do is that I divide the entire text of the DIV, using the space as the delimiter character. In your example, the entire text of the DIV looks something like this:
Artículo: AA02079Descripción: V.ASIENTO JENKINS 2 1/2"PVP (Sin IVA): 379.82€
Of all the text, I am only interested in the last. Everything else is too much for me. So what I do is I split the string, using the Split function (more info here)
This returns me an array of several values (in this case up to 10, because the initial value in an array is always at index 0), which are as follows:
Of all, we are interested in the last one, which is the price. We are interested in Matrix(9). And we have been lucky because, as the web is designed right now, we will always be interested in the latest value because it is the price.
To know the last value inside an array, we use the Ubound property
And to call a particular value from an array, it's just using a number. That is, if you write, for example,
Activecell.Value = Matriz(3)
you will get the fourth value. But if you writeActiveCell.Value = Matriz (Ubound(Matriz))
you will always get the last value of the array, regardless of its size.So with all these ideas is how I have designed the code, and this is what I get after executing it:
I hope it helps you, and you can adapt the code to your needs.