【问题标题】:Fill USPS webform dropdown using VBA EXCEL使用 VBA EXCEL 填写 USPS 网络表单下拉列表
【发布时间】:2015-08-27 17:47:59
【问题描述】:

我正在尝试自动填写 USPS 网络表单,它适用于街道地址、城市和邮政编码,但我无法让它填写州下拉菜单。有什么想法吗?

这是我目前拥有的代码:

Sub USPS()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True

IE.Navigate "https://tools.usps.com/go/ZipLookupAction!input.action?mode=1&refresh=true"
Do
DoEvents
Loop Until IE.READYSTATE = 4

Call IE.Document.getElementByID("tAddress").SetAttribute("value", "2 Peabody Terrace")
Call IE.Document.getElementByID("tCity").SetAttribute("value", "Cambridge")
Call IE.Document.getElementByID("sState").SetAttribute("value", "MA")
Call IE.Document.getElementByID("Zzip").SetAttribute("value", "02138")

Set AllHyperLinks = IE.Document.GetElementsByTagName("A")
    For Each hyper_link In AllHyperLinks
    If ID = "lookupZipFindBtn" Then
        hyper_link.Click
        Exit For
    End If
    Next
End Sub

非常感谢您的帮助!

【问题讨论】:

    标签: excel vba webforms


    【解决方案1】:

    对您的代码进行此更改:

    'Call IE.Document.getElementByID("sState").SetAttribute("value", "MA")
    With IE.Document.getElementByID("sState")
        For i = 0 To .Length - 1
            If .Item(i).Value = "MA" Then
                .Item(i).Selected = True
                Exit For
            End If
        Next
    End With
    

    这将起作用...但网页仍将显示为 Select 元素未更改。这是一种错觉。我怀疑页面中有一些 JavaScript 干扰了该元素的显示。

    但是,如果您在 VBEditor 的即时窗口中执行此操作:

    ?IE.Document.getElementByID("sState").value
    

    ...您会看到控件的值确实已更改为“MA”。

    此外,如果您继续并单击网页的“查找”按钮,您将看到“MA”实际上包含在“您输入:”下。

    所以我上面的代码可以解决你的问题。

    【讨论】:

    • 非常感谢!它似乎正在工作,但不是因为状态发生了变化,而是因为网站不需要状态(即使它说需要)。所以当我点击查找时,它会抛出没有状态的地址,并使用邮政编码找到正确的位置。
    • 那太好了。但是你是说上面的代码没有导致MA 进入服务器?它当然在这里工作。同样,它不会改变表单的外观,但会改变控件的实际值......它会被传递给服务器。
    • 是的,你是对的。我重新检查了它,它正在工作!
    【解决方案2】:

    CSS 选择器:

    您可以使用 css 选择器来执行此操作。

    ①我使用的第一个选择器是为初始登陆页面选择search by address

    #zip-lookup-app a > span
    

    这表示span 标记在a 标记内,在ID 为zip-lookup-app 的元素内部。 "#" 表示 id。

    这符合以下,我想要第一个。

    ②第二个选择器:

    #tState option[value='MA']
    

    这表示选择带有属性valueoption 标记,其值为'MA',在ID 为tState 的元素内。 "[]" 表示属性。


    VBA:

    由于我想要第一个匹配项,我可以使用documentquerySelector 方法来应用CSS 选择器,如下所示:

    Option Explicit
    Public Sub MakeStateSelection()
        Dim ie As New InternetExplorer, html As HTMLDocument
        With ie
            .Visible = True
            .navigate "https://tools.usps.com/go/ZipLookupAction!input.action?mode=1&refresh=true"
    
            While .Busy Or .READYSTATE < 4: DoEvents: Wend
    
            Set html = .document
            html.querySelector("#zip-lookup-app > div > div:nth-child(1) > div > ul > li:nth-child(1) > a > span").Click
    
            While .Busy Or .READYSTATE < 4: DoEvents: Wend
    
            html.querySelector("#tState option[value='MA']").Selected = True
    
           'other code
            Stop '<== Delete me
    
            '.Quit '<== Uncomment me
        End With
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2020-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-13
      • 1970-01-01
      • 2013-09-23
      • 2018-04-24
      相关资源
      最近更新 更多