使用vba从网页上的下拉列表中选择值

时间:2017-01-17 03:27:08

标签: vba excel-vba webautomation excel

this site上我可以从下拉菜单中选择国家/地区和语言,但是当我点击"完成新的申请表"按钮。它说字段是空的。

任何帮助都将不胜感激。

Sub Test()

strURL = "https://visa.kdmid.ru/PetitionChoice.aspx"

  With ie
    .Visible = True
    .navigate strURL

    While .Busy
        DoEvents
    Wend

    Set html = .document

    'Country where you will apply for visa.
    Set ctY = html.getElementById("ctl00$phBody$Country")
    For i = 1 To ctY.Options.Length
        If ctY.Options(i).Text = "NETHERLANDS" Then
            ctY.selectedIndex = i
            Exit For
        End If
    Next i

    'Select Language
    Set lnG = html.getElementById("ctl00$phBody$ddlLanguage")
    For i = 1 To lnG.Options.Length
        If lnG.Options(i).Text = "ENGLISH" Then
            lnG.selectedIndex = i
            Exit For
        End If
    Next i

    'Click I have read instructions check box
    html.getElementById("ctl00$phBody$cbConfirm").Click


    'Click apply button
    Set btnGo = html.forms(0).all("ctl00$phBody$btnNewApplication") 
    btnGo.Click

  End With

  End Sub

1 个答案:

答案 0 :(得分:1)

所以你走在正确的轨道上但是如果你看一下网站的HTML,实际上有两个国家选择的元素 - 你得到了第一个,' ctl00_phBody_Country',但这实际上只是下拉,实际选择的值存储在< ctl00_phBody_cddCountry_ClientState' ...语言部分具有类似的结构。最后,接受的值不仅仅是您在下拉列表中看到的国家/地区名称,它实际上是下拉列表中的国家/地区代码和国家/地区名称的组合....

请参阅下面的示例代码:

Public Sub Test()
Dim IE As InternetExplorer
Dim HTMLDoc As HTMLDocument

Dim countryStr As String
Dim countryObj As HTMLObjectElement
Dim countryCodes As IHTMLElementCollection
Dim codeCounter As Long
Dim languageStr As String
Dim languageObj As HTMLObjectElement
Dim languageCodes As IHTMLElementCollection

countryStr = "Netherlands"
languageStr = "English"

Set IE = New InternetExplorer

With IE
    .Visible = False
    .Navigate "https://visa.kdmid.ru/PetitionChoice.aspx?AspxAutoDetectCookieSupport=1"
    While .Busy Or .ReadyState <> READYSTATE_COMPLETE: Wend
    Set HTMLDoc = IE.document
End With

Set countryObj = HTMLDoc.getElementById("ctl00_phBody_cddCountry_ClientState")
Set countryCodes = HTMLDoc.getElementById("ctl00_phBody_Country").getElementsByTagName("option")
For codeCounter = 0 To countryCodes.Length - 1
    If countryCodes(codeCounter).innerText = UCase(countryStr) Then
        countryObj.Value = countryCodes(codeCounter).Value & ":::" & countryCodes(codeCounter).innerText & ":::"
        While IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE: Wend
        Exit For
    End If
Next

Set languageObj = HTMLDoc.getElementById("ctl00_phBody_cddLanguage_ClientState")
Set languageCodes = HTMLDoc.getElementById("ctl00_phBody_ddlLanguage").getElementsByTagName("option")
For codeCounter = 0 To languageCodes.Length - 1
    If languageCodes(codeCounter).innerText = UCase(languageStr) Then
        languageObj.Value = languageCodes(codeCounter).Value & ":::" & languageCodes(codeCounter).innerText & ":::"
        While IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE: Wend
        Exit For
    End If
Next

HTMLDoc.getElementById("ctl00$phBody$cbConfirm").Click
While IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE: Wend
HTMLDoc.getElementById("ctl00_phBody_btnNewApplication").Click      'Launch Form

IE.Quit
Set IE = Nothing
End Sub