Here's an updated snippet of my project. Place a Button and WebView2 control on a Form and test code below.
I need to load multiple URL's and "scrape" data from each URL one at a time. Unfortunately, WebView2 requires the URL to be loaded from the UI thread, so I can't use threading. This is problematic because the UI won't load the URL until the Button1_Click event is done executing. As a workaround, I tried Application.DoEvents, but my code crashes when processing the 3rd URL, why?
I'm confused and need help. I want to force WebView2 to fully load a URL, run a series of functions to get the data from the page, save to database, and continue to process the next URL. How do I do that?
Imports System.Threading.Tasks
Imports Microsoft.Web.WebView2.Core
Public Class Form1
Private _isNavigating As Boolean
Public Sub New()
InitializeComponent()
Me.InitializeAsync()
End Sub
Private Async Sub InitializeAsync()
Await Me.WebView21.EnsureCoreWebView2Async
End Sub
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim uris As New List(Of Uri)
uris.Add(New Uri("https://qpublic.schneidercorp.com/Application.aspx?AppID=794&PageTypeID=4&KeyValue=C035000070032000"))
uris.Add(New Uri("https://qpublic.schneidercorp.com/Application.aspx?AppID=794&PageTypeID=4&KeyValue=0049B00000026000"))
uris.Add(New Uri("https://qpublic.schneidercorp.com/Application.aspx?AppID=794&PageTypeID=4&KeyValue=X006000000030000"))
uris.Add(New Uri("https://qpublic.schneidercorp.com/Application.aspx?AppID=794&PageTypeID=4&KeyValue=8000000371000"))
For Each uri As Uri In uris
Me.WebView21.CoreWebView2.Navigate(uri.ToString)
' force WebView2 to load webpage before continuing
Me._isNavigating = True
Do
Application.DoEvents()
Loop While Me._isNavigating
Next
End Sub
Private Async Function GetAPN(webView As WebView2) As Task(Of String)
Dim apn As String = Await webView.ExecuteScriptAsync("document.querySelector('#ctlBodyPane_ctl00_ctl01_lblParcelID').innerText;")
If apn = "null" Then
Throw New Exception("APN not found in HTMLDocument")
Else
apn = Trim(apn.Replace("""", ""))
Return apn
End If
End Function
Private Async Function GetAddress(webView As WebView2) As Task(Of String)
Dim address As String = Await webView.ExecuteScriptAsync("document.querySelector('#ctlBodyPane_ctl00_ctl01_lblLocationAddress').innerText;")
If address = "null" Then
Throw New Exception("Address not found in HTMLDocument")
Else
address = Trim(address.Replace("""", ""))
Return address
End If
End Function
Private Sub WebView21_NavigationStarting(sender As Object, e As CoreWebView2NavigationStartingEventArgs) Handles WebView21.NavigationStarting
Debug.Print("NavigationStarting")
End Sub
Private Async Sub WebView21_NavigationCompleted(sender As Object, e As CoreWebView2NavigationCompletedEventArgs) Handles WebView21.NavigationCompleted
Debug.Print("NavigationCompleted")
Me._isNavigating = False
' get data from multiple elements on loaded webpage
Debug.Print("APN: " & Await Me.GetAPN(Me.WebView21))
Debug.Print("Address: " & Await Me.GetAddress(Me.WebView21))
End Sub
End Class