使用vba从网页的下拉列表中选择值 [英] Selecting value from a dropdown list on a webpage using vba

查看:648
本文介绍了使用vba从网页的下拉列表中选择值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此站点上,我可以从下拉菜单中选择国家和语言我点击填写新申请表"按钮.它说字段是空的.

On this site I am able to select the country and language from dropdown menu but when I click on "Complete new application form" button. It says fields are empty.

任何帮助将不胜感激.

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

推荐答案

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

So you are on the right track but if you look at the HTML of the site there are actually two elements with the country selection- you got the first one, 'ctl00_phBody_Country', but this is actually just the drop down, and the actual selected value is stored in 'ctl00_phBody_cddCountry_ClientState'... the language section has similar structure. Lastly the accepted value is not just the country name you see in the drop down, it is actually a combination of a country code from the drop down and the country name....

请参见下面的示例代码:

See below for sample code:

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

这篇关于使用vba从网页的下拉列表中选择值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆