我正在用Excel(VBA)开发宏,以将一些信息从Excel电子表格加载到Internet Explorer(URL:https://www.tarifasdepedagios.com.br/tarifas/inicial),单击“计算”按钮,然后从IE到Excel电子表格获取结果。] >
但是,我无法将结果数据从Internet Explorer传输到Excel!
URL:https://www.tarifasdepedagios.com.br/tarifas/inicial
示例:
输入数据:
结果:
在VBA中无法通过“ getElementsbyID”获得这些结果吗?当前错误与HTML代码中的“ ng-content”有关吗?
有人可以帮我吗?
谢谢你!
链接HTML代码:https://i.stack.imgur.com/AWoEW.png
VBA代码在下面:
Sub Pesquisa_Informações_Rota_TarifasdePedagios()
Dim IE As InternetExplorer, CidadeOrig As String, sng As Date
Dim LR As Long, Contador As Long, CidadeDest As String
Dim RS_litro As String
Dim km_litro As String
Dim Percentual As Single
'Limpa os resultados da última pesquisa realizada
Sheets("tarifasdepedagios.com.br").Select
Range("C5:F5").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("A1").Select
'Identifica a última célula ativa da lista
Sheets("tarifasdepedagios.com.br").Select
LR = Cells(Rows.Count, 1).End(xlUp).Row
'Cria um objeto Internet Explorer
Set IE = New InternetExplorer
'Torna o objeto visível
IE.Visible = True 'True ou False
'Faz um loop por todas as linhas da planilha
For Contador = 5 To LR
'Navega ao site do Tarifas de Pedagios
IE.Navigate "https://www.tarifasdepedagios.com.br/tarifas/inicial"
'Identifica se a página já foi totalmente carregada
While IE.readyState <> READYSTATE_COMPLETE
Wend
sng = Timer
Do While sng + 3 > Timer
Loop
'Carrega os dados de cidade de origem e destino que serão preenchidos na página
RS_litro = Range("B1").Value
km_litro = Range("B2").Value
CidadeOrig = Range("A" & Contador).Value
CidadeDest = Range("B" & Contador).Value
'Carrega os dados de cidade de origem e destino na página e submente os dados do formulário
IE.document.all.Item("mat-input-9").Click
IE.document.all("mat-input-9").Value = CidadeOrig
IE.document.all.Item("mat-input-10").Click
IE.document.all("mat-input-10").Value = CidadeDest
IE.document.all("mat-input-0").Value = RS_litro
IE.document.all("mat-input-1").Value = km_litro
IE.document.all.Item("botaoRoteirizar").Click
'Identifica se a página já foi totalmente carregada
While IE.readyState <> READYSTATE_COMPLETE
Wend
sng = Timer
Do While sng + 3 > Timer
Loop
'Retorna os valores de distância, tempo, pedágio e combustível nas colunas 'C', 'D', 'E' e 'F'
Cells(Contador, 3) = IE.document.getElementsById("mat-input-3")(0).Value <- ERROR HERE (Run-time error '438' Object doesn't support this property or method)
Cells(Contador, 4) = IE.document.getElementsById("mat-input-4")(0).Value
Cells(Contador, 5) = IE.document.getElementsById("mat-input-6")(0).Value
Cells(Contador, 6) = IE.document.getElementsById("mat-input-5")(0).Value
Percentual = (Contador - 4) / (LR - 4)
AtualizaBarra Percentual
Next Contador
IE.Quit
frmProcesso.Hide
MsgBox "Olá, " & Application.UserName & "!" + vbCrLf + "Pesquisa finalizada!"
End Sub
我正在用Excel(VBA)开发宏,以将一些信息从Excel电子表格加载到Internet Explorer(URL:https://www.tarifasdepedagios.com.br/tarifas/inicial),单击“计算”按钮,然后, ...
该功能应该是getElementById
而不是getElementsById
。它获取HTML中的特定元素,用法可以参考this link。