Find getElementsByTagName("A") and click on it

S_White

New Member
Joined
May 8, 2017
Messages
25
Hello all,
On an exchange, I'm trying to find some information about a delisted instrument based on its ISIN code. The exchange use an iframe, so I have to find a way to go to the right tab with the information.

My code is:
VBA Code:
Sub FindDelisted()
Dim IE As InternetExplorer, HTMLdoc As HTMLDocument, Ifrm As HTMLIFrame, HTMLbtn As Object
Dim i As Long

Set IE = New InternetExplorer
With IE
    .Visible = True
    .navigate "http://mdweb.ngm.se/MDWebFront/quotes.html?locale=en_us&activeTab=ndxQuotes&search=DE000SD340R0"
    While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend
    Set HTMLdoc = .document
End With

Do While Ifrm Is Nothing
    Set Ifrm = HTMLdoc.getElementById("quotesDiv")
    DoEvents
Loop

With Ifrm.all
    Do While .Length = 0
        DoEvents
    Loop
    For i = 0 To .Length - 1
        'Find the 'item' that is equal to 'Delisted Instruments'
        With .Item(i)
            If .innerText = "Delisted Instruments" Then
                'Click the 'A' element
                Set HTMLbtn = .getElementsByTagName("A")(0)
                If Not HTMLbtn Is Nothing Then
                    HTMLbtn.Click
                    Exit For
                End If
            End If
        End With
    Next i
End With
End Sub
When the URL loads, it goes to a tab called Quotes, but I want to go to the next tab called Delisted Instruments. My code finds the tab (at least where the innertext does match), but I'm not able to find the 'A' element so I can click on it?

Anyone who knows how I can move to the right tab? @John_w maybe?
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
If you modify the starting of your code in the following way you will end having the tab "Delisted Instruments" selected:
VBA Code:
#If VBA7 Then       '!!! ON  TOP  OF  THE  VBA  MODULE   !!!!
    Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
#Else
    Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If


Sub FindDelisted()
Dim IE As InternetExplorer, HTMLdoc As HTMLDocument, Ifrm As HTMLIFrame, HTMLbtn As Object
Dim i As Long
Dim AColl As Object, myItm As Object        'New DIMs

Set IE = New InternetExplorer
With IE
    .Visible = True
    .navigate "http://mdweb.ngm.se/MDWebFront/quotes.html?locale=en_us&activeTab=ndxQuotes&search=DE000SD340R0"
    While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend
    Set HTMLdoc = .document
End With
'
'New Block:
Set AColl = HTMLdoc.getElementsByClassName("gwt-TabBarItem")
For Each myItm In AColl
''    Debug.Print myItm.innerText
    If myItm.innerText = "Delisted Instruments" Then
        If ntwo = True Then
            myItm.Click
            ntwo = False
            Exit For
        Else
            ntwo = True
        End If
    End If
Next myItm
Sleep 3000
'more code
'more code
Make sure the Declare instructions are on top of the vba Module, before any sub or Function

Bye
 
Upvote 0
Solution
Hello Anthony,
Been away from my desk a few days, but I would certainly like to thank you for the wonderful solution you've provided - appreciated :)

First, I didn't catch why you have this "ntwo" boolean until I realized that actually, there are two items called "Delisted Instruments" and also that I was supposed to read this outside the iframe, but thanks to you now this make perfectly sense.
 
Upvote 0

Forum statistics

Threads
1,224,829
Messages
6,181,222
Members
453,024
Latest member
Wingit77

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