【问题标题】:Internet Explorer - Fill Input Box with Text Mask Using VBAInternet Explorer - 使用 VBA 用文本掩码填充输入框
【发布时间】:2014-11-03 03:40:06
【问题描述】:

下午好!

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

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

格式:00000-000 类型:数字 最大字符:8 最小字符: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;">

【问题讨论】:

  • 您是如何尝试填写文本框的? IE.Document.getelementbyid("pCepNr").value = "BLAH" 是不设置值还是不显示?
  • 其实我已经尝试了很多方法来设置它,到目前为止都没有奏效。正如你所说,我已经尝试过了,但它会导致“运行时错误 91”。
  • 当你Debug.Print这个值时会发生什么?这会返回相同的错误吗?你甚至可以Set 一个该元素的对象吗?
  • 是的,它返回相同的错误,我不知道我是否可以为该对象设置一个元素,我该如何检查它?对不起,我不是程序员,呵呵只是想让它工作..所以我对 VBA 了解不多..但我觉得奇怪的是,正如你所看到的,我可以填写其他 2 个输入表单(pUsrLg 和 pUsrSn)很好,它们具有相同的类和所有.. 我能注意到的唯一区别是文本掩码。
  • 该元素在您执行的最后一个脚本之前是否存在?

标签: javascript html vba internet-explorer excel


【解决方案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")

我怀疑它会抛出同样的错误。 (编辑 请参阅this question 了解Sleep 例程。

除非您查找的元素的 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 秒是可选的,您可以根据需要增加或减少 eg (WaitForElement(IE.Document, "pCepNr", timeout:=10).

【讨论】:

  • 哦,伙计,你是正确的!这是睡眠时间..我强迫了 10 秒的睡眠时间,它工作正常!非常感谢你的帮助,你就是那个人!
猜你喜欢
  • 2015-02-13
  • 1970-01-01
  • 2015-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多