让我首先说,我对网络开发的知识非常有限,所以如果我对这里发生的事情弄错了,请纠正我。
我想要完成的是用VBA复制这表单的手动使用--特别是输入邮政编码并按TAB。
我能够创建一个新的IE实例并使用以下函数导航到页面:
' Get reference to (maybe) already initialized reference
Public Function getSite(url As String) As InternetExplorer
Dim oShell As Object
' curIE is declared as "Private curIE As InternetExplorer" at module level
If curIE Is Nothing Then
Set curIE = New InternetExplorer
curIE.Visible = True
curIE.navigate url
Set oShell = CreateObject("WScript.Shell")
' Probably redundant, could I get some clarification on this?
While (curIE.readyState <> READYSTATE_COMPLETE) Or curIE.Busy ' Wait until it has settled and loaded
Wend
' Maximize the window
Application.Wait Now + TimeValue("00:00:01")
oShell.SendKeys "% "
Application.Wait Now + TimeValue("00:00:01")
oShell.SendKeys "x"
Set getSite = curIE
Else
If curIE.LocationURL <> url Then
curIE.navigate url
End If
Set getSite = curIE
End If
End Function我可以使用这个片段获取ZipCode元素,但我似乎无法完成输入数据的实际工作:
Dim page As HTMLDocument: Set page = getSitePage(curIE.LocationURL) 'Does the work of the GetSite function and gets the document
Dim oShell As Object: Set oShell = CreateObject("WScript.Shell")
Application.Wait Now + TimeValue("00:00:01") 'Seems to help prevent timing issues, is there a better way?
page.getElementsByName("ZipCode").Item(0).Click
oShell.SendKeys "12345{TAB}"我尝试过设置该元素的Value属性,并使用oShell.SendKeys发送一个选项卡,但当该字段的值以这种方式设置时,该字段似乎无法获得焦点。这是这里的关键部分,因为为每个州的每个县创建一个查找功能太费时了,当我知道站点可以自己进行查找时。
如果有一种方法可以使用javascript或其他支持逻辑来执行我试图触发的函数,并且有人可以告诉我如何操作,那就太棒了。太感谢了!
编辑:这里是我试图与之交互的ZipCode字段的源代码:
<input name="ZipCode" class="form-control input-lg valid" id="ZipCode" aria-invalid="false" aria-required="true" aria-describedby="ZipCode-error" type="text" placeholder="Zip Code (Required)" value="" data-val-required="Zip Code is required" data-val="true" data-val-regex-pattern="^\d{5}(-\d{4})?$" data-val-regex="Zip Code format is not valid" autocomplete="false">我不想把这篇文章和整个源弄得乱七八糟,但它可以找到这里。
发布于 2018-06-30 18:09:09
如果你从网站上显示了更多的HTML并解释为什么TAB是必要的,这会有帮助吗?如果要选择另一个字段,则直接引用该元素。
对于要显示的元素,它有一个id,所以请使用它:
page.getElementById("ZipCode").innerText = "myValue"该元素上没有要触发的javascript。
https://stackoverflow.com/questions/34617352
复制相似问题