Search code examples
vbaexcelweb-scrapingbrowser-automationwebautomation

IE Web Automation - How to auto select value from combo box using Excel VBA/XML Macro


I'm a beginner in VBA and I've failed to select country name automatically in web Combo box or list box from my Excel spreadsheet. My code is entering country name only, but not selecting it. How can I change this code so it can pick country name from my Excel spreadsheet and select the same in web combo box as a loop. Passport number, DOB and Nationality are correct on my code. If you'll use manually then you can find the work permit number which I need to capture in my spreadsheet. Chrome Inspect Element screenshot is attached herewith.

Screenshot

My code is as follows:

Sub MOL()
    Dim IE As New SHDocVw.InternetExplorer
    Dim Doc As MSHTML.HTMLDocument
    Dim Buttons As MSHTML.IHTMLElementCollection
    Dim Button As MSHTML.IHTMLElement
    Dim HTMLInput As MSHTML.IHTMLElement
    Dim Tags As MSHTML.IHTMLElement
    Dim HTMLTables As MSHTML.IHTMLElementCollection
    Dim HTMLTable As MSHTML.IHTMLElement
    Dim HTMLRow As MSHTML.IHTMLElement
    Dim HTMLCell As MSHTML.IHTMLElement
    Dim Alltext As IHTMLElementCollection

Application.ScreenUpdating = False
'Application.Calculation = xlCalculationManual
'Application.EnableEvents = False

On Error Resume Next

    IE.Visible = True
    IE.navigate "https://eservices.mol.gov.ae/SmartTasheel/Complain/IndexLogin?lang=en-gb"

Do While IE.readyState <> READYSTATE_COMPLETE: Loop

Set Doc = IE.document
Set Buttons = Doc.getElementsByTagName("Button")
Buttons(2).Click
Do While IE.readyState <> READYSTATE_INTERACTIVE = 3: Loop
Set HTMLInputs = Doc.getElementsByTagName("Input")
    HTMLInputs(46).Value = "somevalue"
    HTMLInputs(48).Value = "24/02/1990"
    HTMLInputs(47).Value = "India"
Buttons(21).Click
End Sub

Solution

  • The solution you look for is a bit difficult to provide. There are few tricky parts to hurdle to select the NATIONALITY from dropdown. I've used .querySelector() within the script to make it concise. However, it should serve your purpose no matter whatever country you wanna select from dropdown. Give it a shot:

    Sub GetInfo()
        Dim IE As New InternetExplorer, HTML As HTMLDocument, post As Object, URL$
    
        URL = "https://eservices.mol.gov.ae/SmartTasheel/Complain/IndexLogin?lang=en-gb"
    
        With IE
            .Visible = True
            .navigate URL
            While .Busy = True Or .readyState < 4: DoEvents: Wend
            Set HTML = .document
    
            HTML.getElementById("TransactionInfo_WorkPermitNumber").innerText = "2659558"
            HTML.querySelector("button[ng-click='showEmployeeSearch()']").Click
    
            Application.Wait Now + TimeValue("00:00:03")  ''If for some reason the script fails, make sure to increase the delay
    
            HTML.getElementById("txtPassportNumber").Value = "J2659558"
            HTML.getElementById("Nationality").Focus
            For Each post In HTML.getElementsByClassName("ng-scope")
                With post.getElementsByClassName("ng-binding")
                    For I = 0 To .Length - 1
                        If .item(I).innerText = "INDIA" Then ''you can change the country name here to select from dropdown
                            .item(I).Click
                            Exit For
                        End If
                    Next I
                End With
            Next post
            HTML.getElementById("txtBirthDate").Value = "24/02/1990"
            HTML.querySelector("button[onclick='SearchEmployee()']").Click
        End With
    End Sub
    

    Reference to add to the library:

    Microsoft Internet Controls
    Microsoft HTML Object library
    

    When you execute the above script, it should give you the desired result.

    Another way would be to go for using xmlhttp request which is way faster than IE. You need to pass the query string parameter arguments as dictionary through "POST" request. If you want to change the parameter as in, birth date,passportor nationality just do it in the QueryString. Btw, the Nationality parameter should be filled in with value instead of name as in, 100 for INDIA. This is how your script should look like:

    Sub Get_Data()
        Dim res As Variant, QueryString$, ID$, Name$
    
        QueryString = "{""PersonPassportNumber"":""J2659558"",""PersonNationality"":""100"",""PersonBirthDate"":""24/02/1990""}"
    
        With New XMLHTTP
            .Open "POST", "https://eservices.mol.gov.ae/SmartTasheel/Dashboard/GetEmployees", False
            .setRequestHeader "User-Agent", "Mozilla/5.0"
            .setRequestHeader "Content-Type", "application/json"
            .send QueryString
            res = .responseText
        End With
    
        ID = Split(Split(Split(res, "Employees"":")(1), "ID"":""")(1), """,")(0)
        Name = Split(Split(Split(res, "Employees"":")(1), "OtherData2"":""")(1), """}")(0)
    
        [A1] = ID: [B1] = Name
    End Sub
    

    Reference to add to the library:

    Microsoft XML, V6.0
    

    Running the above script, you should get the NAME and ID of your required search.