Forum Discussion
StanL
Jan 19, 2025Copper Contributor
WebView2 HTML web-scraping
Interested in a Powershell winform embedding a Webview2 control but with web-scraping capability. Initial frustration trying to find a Microsoft.Web.WebView2.WinForms.dll file that would load in a sc...
luchete
Jan 28, 2025Iron Contributor
Hello StanL
Here's how you can adjust your code to properly execute the script and retrieve the HTML content.
First, make sure you are correctly executing the JavaScript to retrieve the HTML:
$WebView2.add_NavigationCompleted({
$htmlContent = $WebView2.CoreWebView2.ExecuteScriptAsync("document.documentElement.outerHTML").Result
Write-Host $htmlContent
})
This method will execute the JavaScript document.documentElement.outerHTML inside the WebView2 control, which should return the full HTML content of the page.
You can also ensure you're using the right event, like NavigationCompleted, to ensure that the page is fully loaded before attempting to scrape the content.
Let me know how it goes!