Search code examples
vbaexcelwebautomation

Selecting value from a dropdown list on a webpage using 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.

Any help would be appreciated.

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

Solution

  • 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