【发布时间】:2017-06-14 12:58:01
【问题描述】:
我正在尝试将股价的最新价格从
复制到 Excel 工作表中<div id="last-price-value" vkey="LastPrice">127.36</div>.
Internet Explorer 打开并导航到该站点,但最后一个价格没有复制到我的工作表中。我添加了Debug.Printelement.innertext,但立即窗格中没有出现任何内容。
Dim element As IHTMLElement
Dim elements As IHTMLElementCollection
Dim ie As New InternetExplorer
Dim html As HTMLDocument
my_page = "http://www.morningstar.com/stocks/XASX/COH/quote.html"
With ie
.Visible = TRUE
.Navigate my_page
Do While ie.readyState <> READYSTATE_COMPLETE
Loop
Set html = ie.document
Set element = html.getElementById("last-price-value")
Debug.Print element.innertext
Sheets("Sheet 1").Range("A2").Value = element.innertext
End With
【问题讨论】:
-
在您的代码指向的页面中,没有 ID 设置为 last-price-value 的元素。可以通过网页浏览器打开页面查看,打开网页工具,进入控制台执行
document.getElementByID('last-price-value'),会返回null。所以问题出在你的选择器上,而不是你的代码上。 -
谢谢。我正在尝试复制此元素的内部文本:127.36。这是否意味着我需要将代码更改为 getElementsByCaseName()?谢谢
-
您的问题是元素在 iframe 内。不能直接引用,首先得拿到iframe的文档,然后才能继续。现在,我不知道该怎么做,但应该没那么难,你可以开始here。
标签: html excel vba getelementbyid