Excel VBA无法“点击” IE HTML上的按钮

时间:2019-06-12 19:39:22

标签: html excel vba

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

在葡萄牙语中,“领事”表示浏览。

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

<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

有人可以帮我触发该“领事”按钮吗?

1 个答案:

答案 0 :(得分:0)

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

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