I have this code to get all the DIVs based on a CSS selector:
param(
[string]$File
)
# Read the HTML file
$htmlFile = Get-Content -Path $File -Raw
$html = New-Object -Com "HTMLFile"
try
{
# This works in PowerShell 4
$html.IHTMLDocument2_write($htmlFile)
}
catch
{
# This works in PowerShell 5
$html.write($htmlFile)
}
# Select all DIV elements that match the XPath expression
$divElements = $html.querySelectorAll("body > div:nth-child(1) > div:nth-child(2) > div:nth-child(1) > main:nth-child(2) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1)")
The problem is the querySelectorAll
method is not available for the $html object.
What's the next possible option to get all the DIVs without using third-party modules to install with Powershell?