VBA代码来触发一个Javascript函数?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (343)

我需要使用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 
提问于
用户回答回答于

尝试使用dispatchEvent来触发Javascript onkeypress事件。

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

代码:

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

扫码关注云+社区

领取腾讯云代金券