【问题标题】:VBA code to trigger a Javascript function用于触发 Javascript 函数的 VBA 代码
【发布时间】:2016-06-26 06:00:23
【问题描述】:

我需要使用 VBA 自动填写网络表单。当我手动填写邮政​​编码字段(htmIntCEP)时,它会触发一些填充的Javascript代码,以及其他一些字段(htmStrCidade、htmStrEndereco等)。 当我尝试在 VBA 中制作 tha 时,就会出现问题。我可以填写邮政编码字段,但看不到 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 

【问题讨论】:

    标签: javascript vba web-scraping


    【解决方案1】:

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

    这是基于元素具有与之关联的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
    

    【讨论】:

      猜你喜欢
      • 2011-07-13
      • 2021-12-10
      • 2018-02-11
      • 1970-01-01
      • 2012-07-20
      • 2013-09-25
      • 2013-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多