【发布时间】:2016-04-09 14:41:43
【问题描述】:
首先让我说我对 Web 开发的了解非常有限,所以如果我对这里发生的事情有误,请纠正我。
我想要完成的是复制手动使用 this 表单 - 特别是输入邮政编码并按 TAB - 使用 VBA。
我可以创建一个新的 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
而且我可以用这个sn-p得到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">
我不想把这篇文章和整个源代码弄得乱七八糟,但可以找到 here。
【问题讨论】:
-
我不认为选项卡是触发器,它将退出文本框 page.getElementsByName("ZipCode").Item(0).innerText="12345" & poss Chr() for标签???还是退出控件???
-
我想我明白了,所以您认为该事件是通过退出该字段触发的。如果是这样,我如何获得焦点、添加数据并退出该字段?您拥有的代码本质上与我上面的代码具有相同的结果:输入了数字,但它不响应条目或选项卡。
-
你根本不需要使用
SendKeys(),你需要学习如何使用DOM对象——你能提供你想要完成的表单的源代码吗? @Nathan_Sav 仅供参考 - 您可以将vbTab常量用于选项卡;) -
@MacroMan 我已经发布了该特定字段的来源并链接到编辑中的页面。
-
干杯 @MacroMan,我没有写代码,只是想用 OP 激发一些想法 :)
标签: javascript vba excel internet-explorer-11