[RESOLVED]Why can't I programatically fetch HTML from Webview2 (need to click button)
I can successfully fetch the inner text from google.com/index.html using the code below, but must click on a button. However, I would rather invoke an Await somehow to set the value of the sHTML string immediately after initbrowser(). However, I have tried using an Async Function "name" As Task, and awaited for sHTML to be set, but the CoreWebView2 is nothing (null), which throws an exception. Is there is a way to set the value of sHTML programatically right after I initialize the browser?
Code:
Private Async Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Await initasync()
Await initbrowser()
End Sub
Async Function initasync() As Task
Await WebView21.EnsureCoreWebView2Async(Nothing)
End Function
Async Function initbrowser() As Task
Await initasync()
WebView21.CoreWebView2.Navigate("https://www.google.com/index.html")
End Function
Private Async Sub WebNavigation_Complete()
Dim sHTML As String = Await WebView21.ExecuteScriptAsync("document.body.innerText;")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
WebNavigation_Complete()
End Sub
Re: Why can't I programatically fetch HTML from Webview2 (need to click button)
Look at the events for the control... see if there is a Completed event that you can handle, in there, check the state, and if it's done, then you can access the contents.
-tg
Re: Why can't I programatically fetch HTML from Webview2 (need to click button)
Take a look at this example:
Code:
Imports Microsoft.Web.WebView2.Core
Public Class Form1
Private Async Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Await WebView21.EnsureCoreWebView2Async(Nothing)
WebView21.CoreWebView2.Navigate("https://www.google.com/index.html")
Dim javascript = "window.addEventListener('load', () => window.chrome.webview.postMessage(document.body.innerText), false);"
Await WebView21.ExecuteScriptAsync(javascript)
End Sub
Private Sub WebView21_WebMessageReceived(sender As Object, e As CoreWebView2WebMessageReceivedEventArgs) Handles WebView21.WebMessageReceived
Dim content = e.TryGetWebMessageAsString()
Console.WriteLine(content)
End Sub
End Class
It sets up the load event handler using JavaScript, once the window loads it grabs the document's body's innerText and posts it back to the WebView2. The WebView2 then gets the message and prints it to the console.
Re: Why can't I programatically fetch HTML from Webview2 (need to click button)
Quote:
Originally Posted by
dday9
Take a look at this example:
Code:
Imports Microsoft.Web.WebView2.Core
Public Class Form1
Private Async Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Await WebView21.EnsureCoreWebView2Async(Nothing)
WebView21.CoreWebView2.Navigate("https://www.google.com/index.html")
Dim javascript = "window.addEventListener('load', () => window.chrome.webview.postMessage(document.body.innerText), false);"
Await WebView21.ExecuteScriptAsync(javascript)
End Sub
Private Sub WebView21_WebMessageReceived(sender As Object, e As CoreWebView2WebMessageReceivedEventArgs) Handles WebView21.WebMessageReceived
Dim content = e.TryGetWebMessageAsString()
Console.WriteLine(content)
End Sub
End Class
It sets up the load event handler using JavaScript, once the window loads it grabs the document's body's innerText and posts it back to the WebView2. The WebView2 then gets the message and prints it to the console.
Thanks - it worked once during the first debug run, but on the 2nd and 3rd debug runs, it didn't return anything. Is there a handle in windows that needs to be released, or should I dispose something via code? You could try this --> run it under debug once, then quit, then rerun and see what happens.
Re: Why can't I programatically fetch HTML from Webview2 (need to click button)
I'm not able to replicate that behavior. Every time I debug the code it prints the following:
Code:
About
Store
GmailImages
Sign in
Advertising
Business
How Search works
Our third decade of climate action: join us
Privacy
Terms
Settings
Re: Why can't I programatically fetch HTML from Webview2 (need to click button)
Quote:
Originally Posted by
dday9
I'm not able to replicate that behavior. Every time I debug the code it prints the following:
Code:
About
Store
GmailImages
Sign in
Advertising
Business
How Search works
Our third decade of climate action: join us
Privacy
Terms
Settings
Thanks, it only happened once. After new runs, it works like a charm. No issues.