【问题标题】:Excel VBA Select Option from Website Dropdown List while filling webformExcel VBA 在填写网络表单时从网站下拉列表中选择选项
【发布时间】:2019-10-08 12:56:05
【问题描述】:

我正在尝试使用 VBA excel 自动化这个网站。我感到震惊,我必须从下拉框中选择值。我对 VBA 非常陌生,这是我的第一个这样的项目。这是我为选择值而编写的代码。

Sub automaticformfilling_ASDA()

    Dim ie As Object

    Set ie = CreateObject("internetexplorer.application")

    'to make sure that the website is properly loaded

    With ie
        .Visible = True
        .navigate "https://www.argos-pet-insurance.com/quoteAndBuy.do?e=e1s1&curPage=captureDetails&rakT=1510852044896.1391473424.994101.1731.881880349.846|tsid:9904"

        Do While .Busy
            DoEvents
        Loop

        Do While .readyState <> 4
            DoEvents
        Loop  
    End With

    Set Title = i.e.document.getElementById("yourDetailsPolicyHolderTitle")

    For i = 1 To Title.Options.Length
       If Title.Options(i).Text = "Mrs" Then
         Exit For
       End If
    Next i

End Sub

这是该部分的 HTML:

<select name="policyHolder.title" class="select-large" id="yourDetailsPolicyHolderTitle" data-di-field-id="policyHolderTitle">
   <option selected="selected" value="">Please select</option>
   <option value="NWA_PET_T5">Dr</option>
   <option value="NWA_PET_T3">Miss</option>
   <option value="NWA_PET_T1">Mr</option>
   <option value="NWA_PET_T2">Mrs</option>
   <option value="NWA_PET_T4">Ms</option>
</select>

【问题讨论】:

  • 如果您想选择“Mrs”,请使用Title.selectedindex = 4

标签: html excel vba


【解决方案1】:

根据 SJR 的评论以及我在你的代码中发现的一个错字,如果你用这个替换你的代码,那么它应该可以工作:

Sub automaticformfilling_ASDA()

    Dim ie As Object

    Set ie = CreateObject("internetexplorer.application")

    'to make sure that the website is properly loaded

    With ie
        .Visible = True
        .navigate "https://www.argos-pet-insurance.com/quoteAndBuy.do?e=e1s1&curPage=captureDetails&rakT=1510852044896.1391473424.994101.1731.881880349.846|tsid:9904"

        Do While .Busy
            DoEvents
        Loop

        Do While .readyState <> 4
            DoEvents
        Loop
    End With

    Set Title = ie.document.getElementById("yourDetailsPolicyHolderTitle")
    Title.selectedIndex = 4
End Sub

您输入了 i.e.document.getElementByID,您应该在其中输入 ie.document。

【讨论】:

    猜你喜欢
    • 2020-11-25
    • 2018-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2017-05-10
    • 1970-01-01
    相关资源
    最近更新 更多