首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >用于触发Javascript函数的VBA代码

用于触发Javascript函数的VBA代码
EN

Stack Overflow用户
提问于 2016-06-26 14:00:24
回答 1查看 150关注 0票数 1

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

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

有谁有什么线索吗?提前谢谢。

卡洛斯

..。

代码语言:javascript
复制
<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代码的开头

代码语言: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代码:

代码语言:javascript
复制
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 
EN

回答 1

Stack Overflow用户

发布于 2018-06-20 03:58:57

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

这是基于元素有一个与之关联的onkeypress事件的事实:

代码:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38035664

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档