Reputation: 9
I'm trying to take a table (HTML) using the method getElementByID but it doesn't work.
Sub GrabLastNames()
Dim objIE As InternetExplorer
Dim data As Object, ele As Object
Dim aEle As HTMLLinkElement
Dim y As Integer
Set objIE = New InternetExplorer
objIE.Visible = False
objIE.navigate "https://www.rad.cvm.gov.br/ENETCONSULTA/frmGerenciaPaginaFRE.aspx?NumeroSequencialDocumento=93407&CodigoTipoInstituicao=2"
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
y = 1
For Each ele In objIE.document.getElementById("ctl00_cphPopUp_tbDados")
ActiveSheet.Range("A" & y).Value = ele.Children(0).textContent
ActiveSheet.Range("B" & y).Value = ele.Children(1).textContent
ActiveSheet.Range("C" & y).Value = ele.Children(2).textContent
ActiveSheet.Range("D" & y).Value = ele.Children(3).textContent
y = y + 1
Next
objIE.Quit
Set objIE = Nothing
End Sub
Can anyone tell me why? I tryed diferents methods and it's not working.
Upvotes: 0
Views: 312
Reputation: 22440
Try the following to get the tabular content from that webpage. I used a different link within the script to be able to send xhr. You can find the link having static content using dev tools. I also used cookies in order for the script to work correctly. It is way faster then IE. Give it a shot:
Sub fetchTabularData()
Const mainUrl = "https://www.rad.cvm.gov.br/ENETCONSULTA/frmGerenciaPaginaFRE.aspx?NumeroSequencialDocumento=93407&CodigoTipoInstituicao=2"
Const Url$ = "https://www.rad.cvm.gov.br/ENETCONSULTA/frmDemonstracaoFinanceiraITR.aspx?"
Dim elem As Object, tRow As Object
Dim S$, params$, R&, C&
params = "Informacao=2&Demonstracao=4&Periodo=0&Grupo=DFs+Consolidadas&Quadro=Demonstra%c3%a7%c3%a3o+do+Resultado&NomeTipoDocumento=ITR&Empresa=MAGAZ%20LUIZA&DataReferencia=2020-03-31&Versao=1&CodTipoDocumento=3&NumeroSequencialDocumento=93407&NumeroSequencialRegistroCvm=2086&CodigoTipoInstituicao=2"
With CreateObject("WinHttp.WinHttpRequest.5.1")
.Open "GET", mainUrl, False
.send
strCookie = .getAllResponseHeaders
strCookie = Split(Split(strCookie, "Cookie:")(1), ";")(0)
.Open "GET", Url & params, False
.setRequestHeader "Cookie", Trim(strCookie)
.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"
.setRequestHeader "Referer", "https://www.rad.cvm.gov.br/ENETCONSULTA/frmGerenciaPaginaFRE.aspx?NumeroSequencialDocumento=93407&CodigoTipoInstituicao=2"
.send
S = .responseText
End With
With CreateObject("htmlfile")
.body.innerHTML = S
For Each elem In .getElementById("ctl00_cphPopUp_tbDados").Rows
For Each tRow In elem.Cells
C = C + 1: Cells(R + 1, C) = tRow.innerText
Next tRow
C = 0: R = R + 1
Next elem
End With
End Sub
Upvotes: 1
Reputation: 2689
In a well-writeed HTML, there is one and only one tag which id = ctl00_cphPopUp_tbDados
.
Which means your for-loop is not necessary.
You may try
Dim myTbs As Object, myTb As Object, i As Long
Set myTbs = objIE.document.GetElementsByTagname("table")
For Each ele In myTbs
If ele.Rows(0).Cells(0).InnerText = "firstHeader" Then
Set myTb = ele
Exit For
End If
Next
For i = 0 To myTb.Rows.Count-1
ActiveSheet.Range("A" & i).Value = myTb.Rows(i).Cells(0).textContent
ActiveSheet.Range("B" & i).Value = myTb.Rows(i).Cells(1).textContent
ActiveSheet.Range("C" & i).Value = myTb.Rows(i).Cells(2).textContent
ActiveSheet.Range("D" & i).Value = myTb.Rows(i).Cells(3).textContent
Next i
Upvotes: 0
Reputation: 5968
Because the id of the table you are looking for is inside an iFrame.
So you have to look inside the iFrames and not the main document.
This may help : VBA IE Automation - Read iFrame
Upvotes: 1