用于触发Javascript函数的VBA代码

时间:2016-06-26 06:00:24

标签: javascript vba web-scraping

我需要使用VBA自动填写Web表单。当我手动填写邮政​​编码字段(htmIntCEP)时,它会触发一些填充的Javascript代码,其他一些字段(htmStrCidade,htmStrEndereco等)。 当我尝试在VBA中制作时会出现问题。我可以填写邮政编码字段,但我看不到Javascript的调用位置。我会尝试“onblur”事件,但没有这样的事件。

网页中的表单和Javascript(包含文件)如下所示。 (原始网站是:http://seletivo2016.com.br/ler.asp?dir=inscricao&pg=etapa1agendada&enem=1

有没有人有任何线索?提前谢谢。

卡洛斯

...

<tr>
    <td>
        <input name="htmIntCEP" type="text" class="formulario" id="htmIntCEP" size="8" maxlength="8" onkeypress="return m_edit(event, this, '########');" />
        <span><a href="http://m.correios.com.br/movel/buscaCepConfirma.do" target="_blank" tabindex="-1"
            onclick="event.preventDefault(); window.open(this.href, this.target, 'width=320, height=350, left=' + event.x + ', top=' + event.y);">buscar cep</a></span>
    </td>
    <td>
        <input name="htmStrEndereco" type="text" class="formulario" id="htmStrEndereco" onblur="TrimCampo(this)" size="40" maxlength="50" />
    </td>
    <td>
        <input name="htmStrNumero" type="text" class="formulario" id="htmStrNumero" onblur="TrimCampo(this)" size="10" maxlength="10" />
    </td>
</tr>
<tr>
    <td>Complemento:</td>
    <td colspan="2">Bairro:</td>
</tr>
<tr>
    <td>
        <input name="htmStrComplemento" type="text" class="formulario" id="htmStrComplemento" size="20" maxlength="20" onblur="TrimCampo(this)" /></td>
    <td colspan="2">
        <input name="htmStrBairro" type="text" class="formulario" id="htmStrBairro" size="30" maxlength="30" onblur="TrimCampo(this)" /></td>
</tr>
<tr>
    <td>
        <abbr title="Estado">UF</abbr>:</td>
    <td colspan="2">Cidade:</td>
</tr>
<tr>
    <td>
        <input name="htmStrUF" class="formulario" id="htmStrUF" placeholder="Informe o CEP."
            readonly="readonly"/>
    </td>
    <td colspan="2" id="celCidade">
        <input name="htmStrCidade" class="formulario" id="htmStrCidade" placeholder="Informe o CEP."
            readonly="readonly"/>
        <script type="text/javascript" src="/scripts/cep.js"></script>
        <script type="text/javascript">
            $(function () {
                $('#celCidade').parent().parent().parent().parent().cepform('htmIntCEP',
                    'htmStrUF', 'htmStrCidade', 'htmStrEndereco', 'htmStrNumero', 'htmStrBairro', 'htmStrComplemento');
            });
        </script>
    </td>
</tr>

包含JAVASCRIPT代码的开头

(function ($) {
    var server = location.hostname === "localhost" ?
        "localhost:30603" : location.hostname.match(/(fmu\.dev)$/ig) ? "api.fmu.dev/CEP" : "api.fmu.br/CEP";

    var cache = {};

    // ReSharper disable once InconsistentNaming
    function CEP(form, cep, uf, cidade, endereco, numero, bairro, complemento) {
        var api, apic, sending, loader, loading, w, h, self = this;

一段VBA代码:

我想我应该发布一条VBA代码:

Sub test()
    Dim IEApp As InternetExplorer
    Dim HTMLDoc As HTMLDocument
    Dim URL As String
    Dim Formulario As HTMLFormElement

    URL = "http://seletivo2016.com.br/ler.asp?dir=inscricao&pg=etapa1agendada&enem=1"

    Set IEApp = New InternetExplorer
    IEApp.Visible = True
    IEApp.Navigate URL

    Do
        DoEvents
    Loop Until IEApp.ReadyState = 4

    Set HTMLDoc = IEApp.Document
    Set CampoTexto = Formulario.elements("htmIntCEP")
    CampoTexto.Value = "01313010"
    ' Here I thought to put an eventfire 

1 个答案:

答案 0 :(得分:0)

您尝试使用dispatchEvent来触发Javascript onkeypress事件。方法来自John_w。这与.fireEvent相同。

这是基于以下事实:元素具有与之关联的onkeypress事件:

htmInCEP element


代码:

Option Explicit
Public Sub test()
    Dim ieApp As InternetExplorer, htmlDoc As HTMLDocument, URL As String, campoTexto As Object

    URL = "http://seletivo2016.com.br/ler.asp?dir=inscricao&pg=etapa1agendada&enem=1"
    Set ieApp = New InternetExplorer

    With ieApp
        .Visible = True
        .navigate URL

        While .Busy Or .readyState < 4: DoEvents: Wend

        Dim keypressEvent As DOMKeyboardEvent

        Set htmlDoc = .document
        Set CampoTexto = htmlDoc.getElementById("htmIntCEP")
        Set keypressEvent = ie.document.createEvent("KeyboardEvent")
        keypressEvent.initEvent "keypress", True, False
        CampoTexto.Value = "01313010"
        CampoTexto.dispatchEvent keypressEvent
        'Other stuff
        '.Quit
    End With
End Sub