0

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?

quarks
  • 33,478
  • 73
  • 290
  • 513
  • See following : https://stackoverflow.com/questions/56809063/how-to-parse-the-html-of-a-website-with-powershell – jdweng Dec 17 '22 at 15:11

0 Answers0