Re: Interação VBA com HTML, dúvida?
Enviado: Sex Out 20, 2017 7:52 am
não funcionou. vou colocar o modulo aqui pra ver se tem um erro.
eu inspecionei as variáveis no Excel que ele carrega, e la encontrei o botão dentro do item 256("inputelement"), "form"/"all" item 207. Não sei se utilizar isso pra chegar ao botão.
Código: Selecionar todos
'Inclui referência ao Microsoft Internet Controls
Sub lReferenciaIE()
Dim ObRef
On Error Resume Next
ThisWorkbook.VBProject.References.AddFromGuid "{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}", 1, 1
End Sub
Public Function getXPathElement(sXPath As String, objElement As Object) As HTMLBaseElement
Dim sXPathArray() As String
Dim sNodeName As String
Dim sNodeNameIndex As String
Dim sRestOfXPath As String
Dim lNodeIndex As Long
Dim lCount As Long
' Split the xpath statement
sXPathArray = Split(sXPath, "/")
sNodeNameIndex = sXPathArray(1)
If Not InStr(sNodeNameIndex, "[") > 0 Then
sNodeName = sNodeNameIndex
lNodeIndex = 1
Else
sXPathArray = Split(sNodeNameIndex, "[")
sNodeName = sXPathArray(0)
lNodeIndex = CLng(Left(sXPathArray(1), Len(sXPathArray(1)) - 1))
End If
sRestOfXPath = Right(sXPath, Len(sXPath) - (Len(sNodeNameIndex) + 1))
Set getXPathElement = Nothing
For lCount = 0 To objElement.ChildNodes().Length - 1
If UCase(objElement.ChildNodes().Item(lCount).nodeName) = UCase(sNodeName) Then
If lNodeIndex = 1 Then
If sRestOfXPath = "" Then
Set getXPathElement = objElement.ChildNodes().Item(lCount)
Else
Set getXPathElement = getXPathElement(sRestOfXPath, objElement.ChildNodes().Item(lCount))
End If
End If
lNodeIndex = lNodeIndex - 1
End If
Next lCount
End Function
Sub lsPesquisar()
'Inclui a referência se não houver
lReferenciaIE
Dim ie As InternetExplorer
Dim lCidade As String
Dim lUF As String
Dim lUltimaLinhaAtiva As Long
Dim lContador As Long
'Identifica a última célula ativa da lista
lUltimaLinhaAtiva = Worksheets("Plan1").Cells(Worksheets("Plan1").Rows.Count, 1).End(xlUp).Row
'Cria um objeto Internet Explorer
Set ie = New InternetExplorer
'Torna o objeto visível
ie.Visible = True
'Faz um loop por todas as linhas da planilha
For lContador = 2 To lUltimaLinhaAtiva
'Navega ao site
ie.Navigate "https://sistemas.anatel.gov.br/stel/consultas/ListaEstacoesLocalidade/tela.asp?pNumServico=010"
'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 e UF que serão preenchidos na página
lUF = Range("A" & lContador).Value
'Carrega os dados de cidade e UF na página e submente os dados do formulário
ie.Document.all("pSiglaUF").Value = lUF 'coloca os dados da uf
ie.Document.all("botaoFlatConfirmar").Click 'clica em confirmar
While ie.ReadyState <> READYSTATE_COMPLETE
Wend
sng = Timer
Do While sng + 3 > Timer
Loop
Set ele = getXPathElement("/html/body/form/div[2]/table[7]/tbody/tr/td[3]/button", HTMLDoc.getElementById("botaoFlat ExportarExcel"))
If Len(ele) > 0 Then
ele.Click
End If
Next lContador
MsgBox "Concluído!"
End Sub