USPS ZipCode 网站和 Excel VBA - 我拥有用于填充该网站的所有代码,但我无法使用 Excel-VBA 代码单击“查找”提交按钮。
这是该网站的 URL 以及我提交的所有按钮点击代码
https://tools.usps.com/zip-code-lookup.htm?byaddress
'All of the different submit Code I have tried:
html.all.getElementById("zip-by-address").Click
html.all.getelementbytagname("zip-by-address").Click
html.document.querySelector("Find[type=submit]").Click
html.document.getElementsByClassName("btn btn-primary").Click
html.document.getElementById("zip-by-address").Click
For Each Element In ie.document.getElementsByName("zip-by-address")
If Element.className = "btn-primary" Then
Element.Click
End If
Next
ie.document.querySelector("button[type=submit]").Click
For Each Element In ie.document.getElementsByTagName("zip-by-address")
If InStr(Element.innerText, "Find") > 0 Then Element.Click
Next
ie.document.getElementByTagName("Find").FireEvent "onclick"
ie.document.getElementById("zip-by-address").FireEvent "onkeypress"
ie.document.getElementById("zip-by-address").FireEvent "onclick"
Set frm = ie.document.forms(0)
frm.submit
最佳答案
我还必须完成城市和州字段。您无需选择“按地址”选项,因为它已经是网址的一部分。
.querySelector("#zip-by-address").Click
相当于 .getElementById("zip-by-address").Click
但速度更快现代浏览器(咳IE咳)针对css selectors选择进行了优化.
Public Sub ClickFindButton()
Dim ie As SHDocVw.InternetExplorer
Set ie = New SHDocVw.InternetExplorer
With ie
.Visible = True
.Navigate2 "https://tools.usps.com/zip-code-lookup.htm?byaddress" 'url is already by address so no need to click for it
Do
DoEvents
Loop While .Busy Or .ReadyState <> READYSTATE_COMPLETE
With .Document
.querySelector("#tCompany").Value = "The New York Times Company"
.querySelector("#tAddress").Value = "620 Eighth Avenue"
.querySelector("#tCity").Value = "New York"
.querySelector("[value=NY]").Selected = True
.querySelector("#zip-by-address").Click
Stop
End With
.Quit
End With
End Sub
关于excel - 单击 USPS 网站上的按钮查找 9 位数邮政编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60894731/