Results 1 to 6 of 6

Thread: [RESOLVED]Why can't I programatically fetch HTML from Webview2 (need to click button)

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2022
    Posts
    83

    Resolved [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
    Last edited by pel11; Aug 28th, 2023 at 10:53 AM.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2022
    Posts
    83

    Re: Why can't I programatically fetch HTML from Webview2 (need to click button)

    Quote Originally Posted by dday9 View Post
    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.

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    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
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2022
    Posts
    83

    Re: Why can't I programatically fetch HTML from Webview2 (need to click button)

    Quote Originally Posted by dday9 View Post
    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width