Search code examples
htmlpowershelldomgetelementsbytagnamedom-traversal

Find elements on page without searching entire document


To improve performance, I would like to perform getElementsByTagName("td") without having it search the entire document. Is there a way to narrow the scope searching for these elements? Here is my code:

 $ie.Document.getElementsByTagName("td") | ? {($_.className -match 'NodeDocument') 

The problem with this is that it takes over 10 minutes to return the results and I would like to narrow the scope of what is searched so it goes faster and doesn't return unwanted elements.

As an example, I only want to search for the highlighted elements in the image below, and not within the folders above an beneath. Is there a way to specify to only look within the 'Conventional' folder?

Any and all help is appreciated. Thank you.

Thanks for the replies, I will try to implement XPath. For what its worth, here is a screenshot of the HTML. #1 is the element of the 'Conventional' Folder. #2 is the first document element within the folder. enter image description here

Update: Tried Frodo's method of calling .getElementsByTagName() on the $conventional folder in Chrome inspector. Here is a screenshot of result:

enter image description here


Solution

  • Solved. Instead of referencing the $conventional folder (which I found out does not have the "td" NodeDocuments as children), I created a new $DocContainer which points to a div element which DOES have the NodeDocuments as children:

    $DocContainer = $conventional.parentNode.parentNode.parentNode.parentNode.nextSibling
    

    Using this $DocContainer I can now say:

    $documents = $DocContainer.getElementsByTagName("td") | ? {($_.className -match 'NodeDocument')
    

    Special thank you to Frode F. for giving me the thought to reference a new element which is actually the parent container.

    For others with similar issues:

    Take advantage of the chrome inspector / console to test where your elements are within the DOM tree. Chrome's built-in inspector / console is very powerful and can save a lot of time and hassle. Methods like .parentNode, .nextSibling, childNodes are key for DOM traversal. Hope this helps.