[英]Excel VBA: using HTML DOM
我需要知道我可以通過 Excel VBA 使用 HTML 做什么。 例如,我知道我可以通過 id ie.document.getElementByID()
找到元素。 我將使用沒有帶有 id 的元素的 HTML 表,因此它看起來像child->child->sibling->child.....
我想。
任何人都可以向我展示部分代碼,從這個示例表中得到文本“你好”嗎? 第一個節點將通過他的 ID 找到。
...
<table id="something">
<tr>
<td></td><td></td>
</tr>
<tr>
<td></td><td>hello</td>
</tr>
...
我現在正在看這種類型的東西......
我相信像下面這樣的事情應該這樣做:
ie.getelementbyid("something").getelementsbytagname("TD")(3).innertext
這個怎么運作:
它在 HTML 文檔中搜索 ID 等於“某物”的元素(如果超過 1,將進行第一次迭代,但您可以循環遍歷多個)。 然后它將獲取表數據標記並轉到文本所在的迭代 (3)(0 將等於第一個 TD)。
讓我們知道這是否有效。
許多 HTML 文檔在通用表中都有名稱,而不是 ID
我通常使用如下所示的某種形式;
Set HTML = IE.Document
Set SomethingID = HTML.GetElementByID("something").Children
For Each TR in SomethingID
If TR.TagName = "td" Then
If TR.Name = "myname" Then
'code in here if you are looking for a name to the element
ElseIf TR.Value = "myValue" Then
'Code in here if you are looking for a specific value
ElseIf TR.innerText = "mytext" Then
'more code in here if you are looking for a specific inner text
End If
End If
Next TR
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.