Reputation: 25
I would like to write a vba Programm which downloads automaticaly historical stock data from a web-page. The correspindent HTML-Code of the Element I would like to select is on the following Picture: HTML code of the element I would like to click on
My VBA-code Looks as follows:
Dim IE As SHDocVw.InternetExplorer
Dim HTMLDoc As mshtml.HTMLDocument
Dim HTMLCurButton As mshtml.IHTMLElement
Set IE = New SHDocVw.InternetExplorer
IE.Visible = True
IE.FullScreen = False
IE.Resizable = True
IE.Navigate "https://www.dukascopy.com/swiss/english/marketwatch/historical/"
Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = IE.Document
Set HTMLCurButton = HTMLDoc.getElementById(":6n")
HTMLCurButton.Click
But unfortunaly the object HTMLCurButton stays empty.
I am very grateful for any help!
Upvotes: 1
Views: 468
Reputation: 84455
A very interesting problem. I don't think you can solve this with Internet Explorer. This is because the list is in an iframe which is protected by a same origin policy i.e. you cannot navigate to the src of the inner document housing your list.
Using selenium basic vba with Chrome it is possible to switch to the appropriate iframe. You then need a couple of test conditions to be met that allow sufficient time for your actions on the page to take effect.
Option Explicit
Public Sub MakeChanges()
'VBE > Tools > References > Selenium Type Library
'Download: https://github.com/florentbr/SeleniumBasic/releases/tag/v2.0.9.0
Dim d As WebDriver, t As Date
Set d = New ChromeDriver
Const url = "https://www.dukascopy.com/swiss/english/marketwatch/historical/"
With d
.Start "Chrome"
.get url
.SwitchToFrame .FindElementByCss("script + iframe")
t = Timer
Do
With .FindElementByCss("[data-instrument='A.US/USD']")
.Click
If .Attribute("aria-selected") Then Exit Do
End With
Loop While Timer - t < 10
With .FindElementByCss(".d-wh-vg-v-p > div")
If Not .Attribute("aria-disabled") Then .Click
End With
'other code now presented with accept and then login details
Stop
.Quit
End With
End Sub
Upvotes: 1