Problem
Trying to automate a search on this website http://biblioteca.aneel.gov.br/index.html.
Where the search is made on a hidden tab:
- So the hidden tab is named: Legislação
And the html Code:
<a name="lk_legis" class="link_abas" id="lk_leg" onclick="move_layer('legislacao', parent.hiddenFrame.layerX);" href="#">
<span class="text-aba" id="aba_leg">Legislação</span>
</a>
- The field to fill is: Todos os campos
And the HTML Code:
<td><input name="leg_campo1" tabindex="1" class="inputLegEsq" style="margin-top: 0px;" onkeypress="return validaTecla(1,this,event,5,5613,'','parent.hiddenFrame.modo_busca',0)" onchange="atualizaLeg_campos();" type="text" maxlength="500" value=""></td>
- And the Search Button: Busca
And the HTML Code:
<input name="bt_comb" tabindex="20" title="Submeter busca" class="button_busca" style="margin-bottom: 5px;" onclick="return Confere(5613,5,'',parent.hiddenFrame.modo_busca)" type="button" value="Buscar">
<input name="bt_limpar" tabindex="21" title="Limpar campos" class="button_busca" onclick="Resetar();atribuiLegs();" type="button" value="Limpar">
Atempts to Solve
So I tried to solve with the code below:
'Declara função Sleep
#If VBA7 Then
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr) 'For 64 Bit Systems
#Else
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 'For 32 Bit Systems
#End If
Sub CDE_ANEEL()
Dim IE As Object
Dim doc As Object, doc1 As Object, doc2 As Object, aba As Object
Dim el
Set IE = CreateObject("InternetExplorer.Application")
IE.navigate "http://biblioteca.aneel.gov.br/index.html"
IE.Visible = True
EsperaIE IE, 2500
Set doc = IE.document.getElementsbyTagName("frame")(0).contentdocument.body
Set doc1 = doc.getElementsbyClassName("inputLegEsq")
Set doc2 = doc.getElementsbyClassName("button_busca")
Set aba = doc.getElementsbyClassName("text-aba")
For Each el In aba
'Debug.Print el.InnerText
If el.InnerText = "Legislação" Then el.Click
Next el
For Each el In doc1
'Debug.Print el.Name, el.Value
If el.Name = "leg_campo1" Then
el.Value = "Conta de Desenvolvimento Energético"
el.InnerText = "Conta de Desenvolvimento Energético"
el.Click
el.Focus
End If
Next el
'Aperta Enter
Sleep 5000
'Application.SendKeys ("~"), True
'Apertar Botão
For Each el In doc2
Debug.Print el.Title, el.onclick
Debug.Print InStr(1, el.onclick, "Confere(5613,5,'',parent.hiddenFrame.modo_busca)")
If InStr(1, el.onclick, "Confere(5613,5,'',parent.hiddenFrame.modo_busca)") > 0 Then
el.Click
Exit For
End If
Next el
End Sub
Public Sub EsperaIE(IE As Object, Optional time As Long = 250)
'Código de: https://stackoverflow.com/questions/33808000/run-time-error-91-object-variable-or-with-block-variable-not-set
Dim i As Long
Do
Sleep time
Debug.Print CStr(i) & vbTab & "Ready: " & CStr(IE.READYSTATE = 4) & _
vbCrLf & vbTab & "Busy: " & CStr(IE.Busy)
i = i + 1
Loop Until IE.READYSTATE = 4 Or Not IE.Busy
End Sub
However, When the button to search is Clicked. It isn't recognizing as having Data on the Field.
I think it is due to the value onchange function. That is fired only when something is typed and not when the value changes...
I could solve pressing enter with: Application.SendKeys ("~"), True
.
But I want to make IE.visible=False
. So how to search it correctly without having to press Enter?
p.s.: I am trying to make on VBA, because of another functions. Some people said to use Selenium VBA, however, I never used it.