首页 > 解决方案 > Excel VBA 无法在 IE HTML 上“单击”按钮

问题描述

我使用宏在网站上执行搜索。但是,我无法通过宏按下“Consultar”按钮。

在葡萄牙语中,“顾问”的意思是浏览。

我需要触发的按钮的页面源代码部分如下:

<td style="border: none;">
  <a
    id="Link"
    href="#"
    onmousedown="$('#table-consultar').hide(); mostrarBarra();"
    style="margin-right:5px"
    type="button"
    onclick="mojarra.jsfcljs(document.getElementById('form'),{'Link':'Link'},'');return false"
    class="button2"
    >
    <span>Consultar</span>
  </a>
</td>
<div
  class="box boxBlue"
  style="padding:10px !important; width: 760px !important; height: 48px"
>
  <table style="margin: 0px; border: none;" id="table-consultar">
    <tr>
      <td style="border: none; text-align: justify;">
        <div style="float:left">*</div>
        <div style="width: 650px; float:left">
          Para mais praticidade, armazenamos as OPÇÕES ADICIONAIS que você
          seleciona e iremos trazê-las pré-selecionadas em suas consultas. Se
          desejar, basta retirar a seleção e esta não será mais exibida. Estas
          opções estão sujeitas à cobrança adicional, consulte seu<strong>
            contrato.</strong
          >
        </div>
      </td>

      <td style="border: none;">
        <a
          id="Link"
          href="#"
          onmousedown="$('#table-consultar').hide(); mostrarBarra();"
          style="margin-right:5px"
          type="button"
          onclick="mojarra.jsfcljs(document.getElementById('form'),{'Link':'Link'},'');return false"
          class="button2"
          ><span>Consultar</span></a
        >
      </td>
    </tr>
  </table>

  <div
    id="barraDeProgresso"
    align="left"
    style="width: 100%; text-align: center; display: none"
  >
    <div style="margin-top: 5px;">
      <strong>Aguarde!</strong>Sua consulta está sendo realizada.<br />
      <img
        id="imagem"
        src="https://sitenet.serasa.com.br/elementos_estrutura/transacional/application/concentre/images/ajax-loader.gif"
      />
    </div>
  </div>
</div>

下面是我使用的宏代码:

Sub Serasa_data(Tipo_doc As String, Num_doc As String)

'IE
Dim IE As SHDocVw.InternetExplorer
Dim htmlDoc As MSHTML.HTMLDocument 'Microsoft HTML Object Library
Dim htmlInput As MSHTML.HTMLInputElement
Dim htmlColl As MSHTML.IHTMLElementCollection
Dim SubmitButton As Object

'TO COPY DATA
Dim rng As Range
Dim tbl As Object
Dim rw As Object
Dim cl As Object
Dim tabno As Long
Dim nextrow As Long
Dim i As Long
Dim j As Integer

'INPUT
Dim login As String
Dim password As String

Worksheets("Parâmetros").Select
login = Range("log_serasa")
password = Range("sen_serasa")

Set IE = New SHDocVw.InternetExplorer
IE.Visible = True
IE.navigate "https://sitenet.serasa.com.br/Logon/autentica"

Do While IE.ReadyState <> 4 Or IE.Busy
     Application.Wait Now + #12:00:02 AM#
Loop

Do While IE.document.ReadyState <> "complete"
    Application.Wait Now + #12:00:02 AM#
Loop

With IE.document
Set htmlDoc = IE.document

.getElementsByName("LOGON")(0).Value = login
.getElementsByName("SENHA")(0).Value = password
.all("acessar").Click

Do While IE.ReadyState <> 4 Or IE.Busy
    Application.Wait Now + #12:00:02 AM#
Loop

Do While IE.document.ReadyState <> "complete"
    Application.Wait Now + #12:00:02 AM#
Loop

.getElementById("tipoDocumentoCnpj").Checked = True

.getElementById("cpfCnpjId").Value = Num_doc

.getElementsByClassName("button2").Click

For Each tbl In htmlDoc.getElementsByTagName("table")
        tabno = tabno + 1
        nextrow = nextrow + 1
        Set rng = Planilha6.Range("B" & nextrow)
        rng.Offset(, -1) = "Table " & tabno
        For Each rw In tbl.Rows
            For Each cl In rw.Cells
                rng.Value = cl.outerText
                Set rng = rng.Offset(, 1)
                i = i + 1
            Next cl
            nextrow = nextrow + 1
            Set rng = rng.Offset(1, -i)
            i = 0
        Next rw
    Next tbl
End If

IE.Quit
End With

Worksheets("Main").Select
End Sub

但是使用该命令.getElementsByClassName (" button2 ").Click不起作用。

我也尝试过使用下面的代码,但都没有奏效:

Set SubmitButton = IE.document.all.Item("Consultar")
SubmitButton.Focus
submit.Children(0).Click
SubmitButton.FireEvent "onclick"
Set SubmitButton = IE.document.getElementsByClassName("button2")
SubmitButton.FireEvent ("onchange")
SubmitButton.Click

有人可以帮我如何触发这个“顾问”按钮吗?

标签: htmlexcelvba

解决方案


在 Javascript 中,该getElementsByClassName函数返回具有所有给定类名的所有子元素的类数组对象。因此,如果您想引用它,请尝试将其设置为应索引为 0 的第一个元素。

我没有对此进行测试,但您的代码应如下所示: Set SubmitButton = IE.document.getElementsByClassName("button2")(0)


推荐阅读