Internet Explorer - 使用VBA填充带文本掩码的输入框

时间:2014-09-09 16:07:01

标签: javascript html vba internet-explorer excel-vba

下午好!

我正在尝试运行一个打开网站的宏,登录,在菜单中选择一个选项,填写一个文本框并单击另一个按钮。

到目前为止,我可以打开网站,登录,在菜单中选择选项,但我无法通过任何方式填写文本框。该文本框有一个文本掩码:

格式:00000-000 类型:数字 Max Char。:8 Min Char。:8

Sub SAVE()


    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    Application.StatusBar = "Searching"


Sheets("WEB").Visible = True

    Dim IE As New InternetExplorer
    Dim WshShell As Object
    Dim dtInicio As Date
    Dim objIE As New DataObject



    Sheets("SAVE").Select
    Sheets("SAVE").Cells.Clear


S = 2
V = 2

SALVA = 0
Do While Sheets("Adress").Cells(V, 2) <> ""
    IE.Visible = True

    Sheets("WEB").Select
    ActiveWindow.SelectedSheets.Delete
    Sheets.Add After:=Sheets(Sheets.Count)
    Sheets(Sheets.Count).Select
    Sheets(Sheets.Count).Name = "WEB"


'OPEN URL
    IE.navigate "URL HERE"

    While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE
        Sleep (1000)
        DoEvents
    Wend



    IE.Document.forms(0).pUsrLg.Value = "user"
    IE.Document.forms(0).pUsrSn.Value = "password"
    IE.Document.parentWindow.execScript "login();"

    While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE
    Sleep (1000)
    DoEvents
    Wend

    IE.Document.parentWindow.execScript "enviaPage('mod_pesquisa/DisponibilidadeNaoClientes.asp', 'GET', 'True', 'dv_pagina', 'pUsrId=0000050007', 'True', 389, 892);"

    While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE
    Sleep (1000)
    DoEvents
    Wend

输入我必须填写如下:

<input name="pCepNr" id="pCepNr" type="text" class="st_cxt_01" style="height: 20px; width: 85px; background-color: #FFEEDD; color: #913A1A;">

1 个答案:

答案 0 :(得分:1)

因为方法IE.Document.getelementbyid("pCepNr").value返回错误代码91 (Object not set),我怀疑.getElementById未能找到包含id="pCepNr"

的HTML元素

请在您的代码中尝试此操作来确认:

' snip ...
IE.Document.parentWindow.execScript "enviaPage('mod_pesquisa/DisponibilidadeNaoClientes.asp', 'GET', 'True', 'dv_pagina', 'pUsrId=0000050007', 'True', 389, 892);"

While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE
    Sleep (1000)
    DoEvents
Wend

Dim problemTextBox as Object
Set problemTextBox = IE.Document.getelementbyid("pCepNr")

我怀疑它会抛出相同的错误。 (编辑 Sleep例程见this question

除非您要查找的元素的ID错误,否则在您尝试访问该元素时该元素根本不存在。

如果您正在运行此脚本,

IE.Document.parentWindow.execScript "enviaPage('mod_pesquisa/DisponibilidadeNaoClientes.asp', 'GET', 'True', 'dv_pagina', 'pUsrId=0000050007', 'True', 389, 892);"

是加载元素并且之前元素不存在的东西,然后你的等待循环无法等待足够长的时间来加载它。

我注意到.asp网页IE.BusyIE.readyState并不总是有效。您可以通过调试器逐步减速并在知道它被加载后访问输入框或仅通过强制大(10秒)睡眠来测试它。

如果只是在加载元素之前退出等待循环,那么您将需要更好的等待例程。这是我使用的一部分,部分来自SE的其他问题。

Public Function ElementExists(document as Object, id As String) As Boolean

    Dim el As Object
    Set el = Nothing

    On Error Resume Next
    Set el = document.getElementById(id)

    On Error GoTo 0
    ElementExists = Not (el Is Nothing)

End Function
Public Function WaitForElement(document as Object, ByVal id As String, Optional ByVal timeout As Integer = 3) As Boolean

    Dim start_time As Single
    start_time = Timer

    Do While Not (ElementExists(document, id)) And start_time + timeout > Timer
        DoEvents
    Loop
    WaitForElement = ElementExists(document, id)

End Function

调用WaitForElement(IE.Document, "pCepNr")将等待不超过3秒的元素加载,并将返回元素是否可访问(true | false)。 3秒是可选的,您可以根据需要增加或减少例如WaitForElement(IE.Document, "pCepNr", timeout:=10)