Vba IE.由于值更改,单击无法正常工作 问题

Vba IE.由于值更改,单击无法正常工作 问题,vba,excel,internet-explorer,Vba,Excel,Internet Explorer,正在尝试自动搜索此网站 在隐藏选项卡上进行搜索的位置: 因此隐藏的标签名为:Legislação 以及html代码: <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</s

正在尝试自动搜索此网站

在隐藏选项卡上进行搜索的位置:

因此隐藏的标签名为:Legislação 以及html代码:

<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>
但是,当单击要搜索的按钮时。它不认为有现场数据

我认为这是由于值onchange函数。只有在键入内容时才会触发,而不是在值更改时触发

我可以用:Application.SendKeys~,True来解决按enter键的问题

但我想让IE.visible=False。那么,如何在不必按Enter键的情况下正确搜索它呢


p、 s:我正试图在VBA上实现,因为还有其他功能。有些人说要使用Selenium VBA,但我从来没有使用过它。

我想你可以试试下面的方法

If el.Name = "leg_campo1" Then
    el.Value = "Conta de Desenvolvimento Energético"
    Set evnt = doc.ownerDocument.createEvent("HTMLEvents")
    evnt.initEvent "change", True, True
    el.dispatchEvent evnt
End If
这来自下面的线程


尝试选择XHR而不是IE。
    <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">
'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
If el.Name = "leg_campo1" Then
    el.Value = "Conta de Desenvolvimento Energético"
    Set evnt = doc.ownerDocument.createEvent("HTMLEvents")
    evnt.initEvent "change", True, True
    el.dispatchEvent evnt
End If