Html 通过标签名 VBA 获取元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21741529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get elements by tag name VBA
提问by Cessna
I'm trying to get the value this_is_the_text_i_need_to_get
out the following HTML source code:
我正在尝试this_is_the_text_i_need_to_get
从以下 HTML 源代码中获取值:
<div class="blubrBoxContent">
<input id="url" type="text" name="__url"
value="this_is_the_text_i_need_to_get">
</div>
This is the VBA code that I've got so far:
这是我到目前为止的 VBA 代码:
Set TDelements = HTMLdoc.getElementsByTagName("div")
For Each TDelement In TDelements
If TDelement.ID = "url" Then
MyVar = TDelement.innerText("Value")
End if
Next
The thing is that I can't make the code to go into the If
statement. It looks like ID
is not a property of the TDelement
.
问题是我无法将代码放入If
语句中。看起来ID
不是TDelement
.
回答by Tim Williams
There is no problem with your HTML (at least, there's no problem because most browsers can deal with mal-formed HTML pretty well since there's so much of it out there)
你的 HTML 没有问题(至少,没有问题,因为大多数浏览器可以很好地处理格式错误的 HTML,因为那里有很多)
Add a reference to Microsoft HTML Object Library
添加引用 Microsoft HTML Object Library
Sub Tester()
Dim doc As New HTMLDocument, els, el
doc.body.innerHTML = "<div class='bluBrdrBox1'> <div class='blubr1ct'></div>" & _
"<div class='blubrBoxContent'><input id='url' type='text' " & _
"name='__url' value='bit.ly/1dJ17Yg'></div>"
;
Set els = doc.getElementsByTagName("input")
For Each el In els
Debug.Print el.ID, el.Value
Next el
End Sub