Learn about VBScript GetElementsByTagName method, including HTML parsing, element selection, and more. Understand how to use it effectively with practical examples.
last modified April 9, 2025
The GetElementsByTagName method in VBScript is part of the DOM (Document Object Model) interface. It retrieves all elements with a specified tag name from an HTML or XML document. This method returns a collection of elements that can be iterated through. It’s commonly used in web scraping and document manipulation.
GetElementsByTagName enables efficient element selection without knowing element IDs or classes. It works with both HTML and XML documents. This tutorial covers GetElementsByTagName with practical examples to demonstrate its usage.
The GetElementsByTagName method takes one parameter: the tag name to search for. It returns a collection of elements matching the specified tag. The method is available on document objects and individual element nodes in VBScript.
Key features include case-insensitive matching in HTML documents and support for wildcard selection. It searches through all descendants of the element it’s called on. Understanding this method helps create robust document processing scripts.
This example demonstrates the simplest use of GetElementsByTagName to select all paragraph elements. It shows how to access the returned collection and display element content. The script processes an HTML string in memory.
basic_tag_selection.vbs
Set htmlDoc = CreateObject(“htmlfile”) htmlDoc.write “<html><body><p>First paragraph</p><p>Second paragraph</p></body></html>”
Set paragraphs = htmlDoc.getElementsByTagName(“p”) For Each para In paragraphs WScript.Echo para.innerText Next
Set htmlDoc = Nothing
The script creates an HTML document with two paragraphs. GetElementsByTagName returns all paragraph elements. The loop displays each paragraph’s text content. This demonstrates basic tag-based element selection.
This example shows how to retrieve attributes from elements selected by tag name. It demonstrates accessing the href attribute of anchor tags. The script processes a simple HTML document with links.
element_attributes.vbs
Set htmlDoc = CreateObject(“htmlfile”) htmlDoc.write “ExampleGoogle”
Set links = htmlDoc.getElementsByTagName(“a”) For Each link In links WScript.Echo “Text: " & link.innerText & “, Href: " & link.href Next
Set htmlDoc = Nothing
The script creates an HTML document with two anchor tags. GetElementsByTagName selects all anchor elements. The loop displays each link’s text and URL. This shows how to work with element attributes.
This example demonstrates selecting elements within a specific container. It shows how to limit the scope of GetElementsByTagName to a div element. The script processes an HTML document with nested structure.
nested_selection.vbs
Set htmlDoc = CreateObject(“htmlfile”) htmlDoc.write “Content paragraph
Outside paragraph
"
Set contentDiv = htmlDoc.getElementById(“content”) Set contentParagraphs = contentDiv.getElementsByTagName(“p”)
For Each para In contentParagraphs WScript.Echo “Content paragraph: " & para.innerText Next
Set htmlDoc = Nothing
The script creates an HTML document with paragraphs inside and outside a div. GetElementsByTagName is called on the div element to select only its child paragraphs. This demonstrates scoped element selection.
This example shows how to process HTML tables using GetElementsByTagName. It demonstrates selecting table rows and cells to extract tabular data. The script processes a simple HTML table structure.
table_processing.vbs
Set htmlDoc = CreateObject(“htmlfile”) htmlDoc.write “<html><body><table><tr><td>Cell 1</td><td>Cell 2</td></tr><tr><td>Cell 3</td><td>Cell 4</td></tr></table></body></html>”
Set rows = htmlDoc.getElementsByTagName(“tr”) rowNum = 1
For Each row In rows Set cells = row.getElementsByTagName(“td”) cellText = "” For Each cell In cells cellText = cellText & cell.innerText & " | " Next WScript.Echo “Row " & rowNum & “: " & cellText rowNum = rowNum + 1 Next
Set htmlDoc = Nothing
The script creates an HTML document with a simple table. GetElementsByTagName first selects all table rows, then cells within each row. The nested loops process the table structure to display cell contents. This shows hierarchical element processing.
This example demonstrates selecting multiple element types in a single operation. It shows how to use the wildcard character (*) with GetElementsByTagName. The script processes an HTML document with various elements.
wildcard_selection.vbs
Set htmlDoc = CreateObject(“htmlfile”) htmlDoc.write “<html><body><h1>Title</h1><p>Paragraph</p><div>Division</div></body></html>”
Set allElements = htmlDoc.getElementsByTagName(”*”) For Each elem In allElements WScript.Echo “Element: " & elem.tagName & “, Content: " & elem.innerText Next
Set htmlDoc = Nothing
The script creates an HTML document with various elements. GetElementsByTagName(”*”) selects all elements in the document. The loop displays each element’s tag name and content. This demonstrates comprehensive document traversal.
In this article, we have explored the GetElementsByTagName method in VBScript, covering its usage and practical applications. From basic element selection to complex document traversal, these examples demonstrate powerful HTML processing. With this knowledge, you can enhance your web scraping and document manipulation scripts.
My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.
List all VBScript tutorials.