Change selection in IE dropdown

WebScraper

New Member
Joined
Aug 21, 2017
Messages
4
Hello,

I'm trying to open a webpage and then change the selection in a dropdown. The page is made in Java so it's quite hard to find IDs and Tags. My code is:

Code:
Sub GetShares()
Dim IE As New InternetExplorer
Dim HtmlDoc As New MSHTML.HTMLDocument
Dim iFrm As HTMLIFrame
Dim DropDown As MSHTML.HTMLInputElement

With IE
    .navigate "http://turing.ngm.se/MDWebFront/quotes.html"
    .Visible = True

    Do While .Busy Or .READYSTATE <> READYSTATE_COMPLETE
        DoEvents
    Loop
    
    Application.Wait (Now + TimeValue("00:00:03"))
    Set HtmlDoc = .document
End With

Set iFrm = HtmlDoc.getElementById("quotesDiv")
Set DropDown = iFrm.document.getElementsByClassName("selected")(0)
DropDown.Value = 3
End Sub

Through debugging I've managed to sort out that the dropdown I would like to change is found using iFrm.document.getElementsByClassName("selected")(0). The reason for using (0) is that there are 5 other dropdowns with the classname "selected" on the page.

What I would like to do is to change the selection in the dropdown from All (73) to the 4th. option called 'Equity Stockholm (11). I expect the base to be 0 hence the index to number 3. When adding a watch to DropDown I can see that its value is 0 but my code doesn't work when I try to change it to 3 though I'm a bit unsure that this is the right way to change the dropdown.

Anyone who can point me in the right direction so I can change the dropdown selection?

Thanks in advance.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Got an update to my inquriy. I've managed to change my selection from the first one, 'All (73)' to the 4th. option 'Equity Stockholm (11)' with my changed code found below, but the change in selection doesn't trigger an update of the dropdown made in Javascript eventhough that I can see it on teh screen when I try to debug?

Anyone who can tell my how I can trigger the update?

Code:
Sub GetShares()
Dim IE As New InternetExplorer
Dim HtmlDoc As New MSHTML.HTMLDocument
Dim iFrm As HTMLIFrame
Dim DropDown As MSHTML.HTMLInputElement

With IE
    .navigate "http://turing.ngm.se/MDWebFront/quotes.html?"
    .Visible = True

    Do While .Busy Or .READYSTATE <> READYSTATE_COMPLETE
        DoEvents
    Loop
    
    Application.Wait (Now + TimeValue("00:00:03"))
    Set HtmlDoc = .document
End With

Set iFrm = HtmlDoc.getElementById("quotesDiv")

Set DropDown = iFrm.document.getElementsByClassName("dropdown-menu inner")(0)
DropDown.Children(0).className = ""
DropDown.Children(3).className = "selected"
With iFrm.document.getElementsByClassName("btn dropdown-toggle btn-default")(0)
    .Focus
    .Click
End With
End Sub

Thanks
 
Upvote 0
You need to reference the dropdown's appropriate LI element and click its child A (anchor) element. This works for me:

Code:
Public Sub IE_Test()

    Dim IE As InternetExplorer
    Dim HTMLdoc As HTMLDocument
    Dim listUL As HTMLUListElement, listLI As HTMLLIElement
    
    Set IE = New InternetExplorer
    With IE
        .Visible = True
        .navigate "http://turing.ngm.se/MDWebFront/quotes.html"
        While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend
        Set HTMLdoc = .document
    End With

    'Get the UL element containing dropdown options
    
    Do
        Set listUL = HTMLdoc.getElementsByTagName("UL")(0)
        DoEvents
    Loop While listUL Is Nothing
    
    'Get the 4th child LI element: Equity Stockholm (11)
    
    Do
        Set listLI = listUL.Children(3)
        DoEvents
    Loop While listLI Is Nothing
    
    'Click its A element
    
    listLI.getElementsByTagName("A")(0).Click
    While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE: DoEvents: Wend
   
End Sub
 
Last edited:
Upvote 0
John,

You're right. I didn't think of using the 'A' as an element with a tag that I could click on.

Sorted! Thanks!

WebScraper
... and btw - my name is Charlie :)
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,201
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top