我想写一个vba程序,它从网页自动下载历史股票数据。我要选择的元素的对应HTML位于以下图片上:我想单击的元素的HTML代码
我的VBA代码如下所示:
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但不幸的是,HTMLCurButton对象仍然是空的。
我非常感谢你的帮助!
发布于 2019-03-03 17:06:14
一个非常有趣的问题。我不认为你能用I解决这个问题。这是因为列表位于受同源政策保护的iframe中,即不能导航到包含列表的内部文档的src。
使用selenium和Chrome,可以切换到适当的iframe。然后,您需要满足几个测试条件,以便有足够的时间让页面上的操作生效。
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 Subhttps://stackoverflow.com/questions/54969907
复制相似问题