Internet Explorer - 使用VBA填充带文本掩码的输入框 [英] Internet Explorer - Fill Input Box with Text Mask Using VBA

查看:298
本文介绍了Internet Explorer - 使用VBA填充带文本掩码的输入框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下午好!



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



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



格式:00000-000
类型:数值
最大值:8
最小字符。:8

  Sub SAVE()


Application.DisplayAlerts = False
Application.ScreenUpdating = False
Application.StatusBar =搜索


表格(WEB)。Visible = True

Dim IE作为新的InternetExplorer
Dim WshShell As Object
Dim dtInicio As Date
Dim objIE As New DataObject



表格(SAVE) 。选择
表单(SAVE)。Cells.Clear


S = 2
V = 2

SALVA = 0
执行表格(地址)。单元格(V,2)
IE.Visible = True

表格(WEB)。选择
ActiveWindow.SelectedSheets.Delete
Sheets.Add After:= Sheets(Sheets计数)
表(Sheets.Count)。选择
表(Sheets.Count).Name =WEB


'OPEN URL
IE .navigateURL HERE

虽然IE.Busy或IE.readyState<> READYSTATE_COMPLETE
睡眠(1000)
DoEvents
Wend



IE.Document.forms(0).pUsrLg.Value =user
IE.Document.forms(0).pUsrSn.Value =password
IE.Document.parentWindow.execScriptlogin();

虽然IE.Busy或IE.readyState<> READYSTATE_COMPLETE
睡眠(1000)
DoEvents
Wend

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

虽然IE.Busy或IE.readyState<> READYSTATE_COMPLETE
睡眠(1000)
DoEvents
Wend

输入我必须填写这样:

 < input name =pCepNrid =pCepNrtype =textclass =st_cxt_01style =height:20px; width:85px; background-color:#FFEEDD; color:#913A1A;> 


解决方案

因为方法 IE。 document.getelementbyid(pCepNr)。值正在返回错误代码91
对象未设置),我怀疑 .getElementById 未能找到一个HTML元素 id =pCepNr



请通过在代码中尝试以下方式来确认:

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

虽然IE.Busy或IE.readyState<> READYSTATE_COMPLETE
睡眠(1000)
DoEvents
Wend

Dim problemTextBox as Object
设置problemTextBox = IE.Document.getelementbyid(pCepNr)

我怀疑它会抛出相同的错误。 (编辑 请参阅此问题 c code code code $ c

除非您要查找的元素的ID不正确当您尝试访问该元素时,该元素根本不存在。



如果您正在运行此脚本,



< blockquote>

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


是什么加载元素,元素不​​存在之前那么您的等待循环无法等待足够长的时间才能加载。



我注意到.asp页面 IE.Busy IE.readyState 不总是工作。您可以通过调试器逐步减速并在您知道加载后访问输入框,或通过强制(10秒钟)的睡眠来测试。



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

 公共功能ElementExists(文档为对象, 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)

结束函数
公共函数WaitForElement(文档为Object,ByVal id As String,可选ByVal timeout As Integer = 3)As Boolean

Dim start_time As Single
start_time = Timer

Do While Not(ElementExists(document,id))而start_time + timeout>定时器
DoEvents
循环
WaitForElement = ElementExists(document,id)

结束函数

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


good afternoon!

I'm trying to run a Macro that opens a website, login, select an option in the menu, fill a text-box and click another button.

So far, I can open the website, login, select the option in the menu, but I can't get the text-box filled by any means. That text box has a text mask:

Format: 00000-000 Type: Numeric 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

The Input I must fill looks like this:

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

解决方案

Because the method IE.Document.getelementbyid("pCepNr").value is returning error code 91 (Object not set), I suspect .getElementById is failing to find an HTML element with id="pCepNr"

Please confirm this by trying this in your code:

' 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")

I suspect it will throw the same error. (EDIT See this question for the Sleep routine.)

Unless you have the wrong id of the element you are looking for, the element simply doesn't exist when you try to access it.

If this script you are running ,

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

is what loads the element and the element doesn't exist before then, then your wait loop is failing to wait long enough for it to load.

I have noticed that for .asp pages IE.Busy or IE.readyState do not always work. You can test that by either stepping through slowing with the debugger and accessing the input box after you know it is loaded or just by forcing a large (10 second) sleep.

If simply your wait loop is exiting before the element is loaded, then you will need a better waiting routine. Here is one that I have used, parts of it are from other questions on 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

Calling WaitForElement(IE.Document, "pCepNr") will wait no more than 3 seconds for the element to load and will return whether the element is accessible (true|false). 3 seconds is optional you can increase or decrease as you see fit eg (WaitForElement(IE.Document, "pCepNr", timeout:=10).

这篇关于Internet Explorer - 使用VBA填充带文本掩码的输入框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆