Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to automate a web navigation. I am stuck at selecting the dropdown. This is what I have so far:

the_Start:
    Dim i As Integer
    Dim objIE As Object
    Set objIE = CreateObject("InternetExplorer.Application")
    objIE.Top = 0
    objIE.Left = 0
    objIE.Width = 800
    objIE.Height = 600
    objIE.Visible = True
    objIE.navigate ("https://suit.cibil.com")

    With objIE
        Do While .Busy
            DoEvents
        Loop
        Do While .ReadyState <> 4
            DoEvents
        Loop
    End With
    
    While objIE.Busy
        DoEvents
    Wend
    
    If objIE.Busy = False Then
    
        Set the_input_elements = objIE.Document.getElementsByTagName("select")
        
        For Each input_element In the_input_elements
        
            If input_element.getAttribute("name") = "croreAccount" Then
                'MsgBox ("Found croreAccount")
                Set option1 = input_element.getElementsByTagName("option")
                For Each dropdown1 In option1
                    If dropdown1.Value = "1" Then
                        'MsgBox dropdown1.Text
                        dropdown1.Focus
                        dropdown1.FireEvent ("onchange")
                        dropdown1.Click
                        dropdown1.Selected = "selected"
                        dropdown1.Selected = True
                    End If
                Next
            End If
            
        Next
        
    End If
End Sub

'This opens the website, and correctly takes me to option tab, but do not select them. Please help as' 'this is extremely repetetive work'


HTML

<select name="croreAccount" class="select" id="croreAccount" style="z-index: 10; opacity: 0;" onchange="changeQuarter(this);">
    <option selected="selected" value="0">Option</option>
    <option value="1">Search</option>
    <option value="2">Summary - Credit Institutions Wise List</option>
    <option value="3">Summary - State/Union Territory</option>
</select>
question from:https://stackoverflow.com/questions/66051020/unable-to-select-dropdown-on-website-in-excel-vba

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
259 views
Welcome To Ask or Share your Answers For Others

1 Answer

Firstly, you can use the id to target the appropriate select element. You don't need any looping as you can then select the required option using its value attribute. After making your selected by setting the option as selected = True, you need to attach a change HTMLEvent to the parent select element and dispatch it so the node registers the options change and updates

Option Explicit

Public Sub test()

    Dim objIE As Object
    
    Set objIE = CreateObject("InternetExplorer.Application")
    
    objIE.Visible = True
    objIE.navigate "https://suit.cibil.com"
    
    Dim evt As Object
    
    With objIE
    
        While .busy Or .ReadyState <> 4: DoEvents: Wend
        
        .document.querySelector("#croreAccount [value='1']").Selected = True
        
        Set evt = .document.createEvent("HTMLEvents")
        evt.initEvent "change", True, False
        
        .document.querySelector("#croreAccount").dispatchEvent evt
        
        Stop

        .Quit

    End With
    
End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...