1
This code clicks on the 'Legislation' tab and fills a field with a given text (2º For Each) and clicks on the search button (3º For Each). The problem is that when I run the code, it clicks on the tab and fills in the text, but it Zera the field before doing the research, and I tried it in two different ways. What should I do?
'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 sFilename As String, sFilepath As String
Dim objStream As Object
Dim strData As String
Dim el
Set objStream = CreateObject("ADODB.Stream")
sFilename = "CDE.txt"
sFilepath = "C:\Users\leandro.lazari\Desktop\Dados MegaWhat\Dados" & "\" & sFilename
Set IE = CreateObject("InternetExplorer.Application")
IE.navigate "http://biblioteca.aneel.gov.br/index.html"
IE.Visible = True
Sleep 2000
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")
'clica em aba 'Legislação'
For Each el In aba
'Debug.Print el.InnerText
If el.innerText = "Legislação" Then el.Click
Next el
'preenche campo: 'Todos os Campos'
For Each el In doc1
'Debug.Print el.Name, el.Value
If el.Name = "leg_campo1" Then el.Value = "Conta de Desenvolvimento Energético"
Next el
'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
Next el
' Apertar o Botão de outra forma
For Each el In doc2
If InStr(1, el.onclick, "Confere(5613,5,'',parent.hiddenFrame.modo_busca)") > 0 Then el.Click
Next el
End Sub
Check that on this site, in the text box you are updating or on the button, there is some associated script that is clearing the value of the box.
– Pagotti
Pagotti, has a Javascript that in the text has p written "autoReset: true". Since I don’t know much about it, I don’t know if this is the issue or how to solve it.
– Leandro Lazari
It is likely that this is causing this behavior. Try to force the event
change
before calling theclick
– Pagotti
And how I do it?
– Leandro Lazari
I don’t know the IE DOM object model via automation like you’re using, but I believe it’s something like making a
el.Focus
before putting thevalue
and ael.Blur
after. Search the IE object model to know the commands you can execute on an element.– Pagotti