0

I have written some VB.Net code using WebView2 control to try to download a PDF file from a specific magazine.

My VB.Net code is following

Imports Microsoft.Web.WebView2.Core

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        Call InitializeAsync()
    End Sub

    Async Sub InitializeAsync()
        Await wv.EnsureCoreWebView2Async()
        wv.CoreWebView2.Navigate("https://journal.cinetelerevue.sudinfo.be")
    End Sub

    Private Sub wv_NavigationCompleted(sender As Object, e As CoreWebView2NavigationCompletedEventArgs) Handles wv.NavigationCompleted
        Threading.Thread.Sleep(1000)
        Call ClickOnPdfButton()
        Threading.Thread.Sleep(1000)
    End Sub

    Async Sub ClickOnPdfButton()
        Dim sButtonCmd = "document.getElementById('readPdfBtn').click();"
        Dim task = Await wv.ExecuteScriptAsync(sButtonCmd)
    End Sub

End Class

The first Navigate() method display correctly requested URL.

The Javascript document.getElementById('readPdfBtn').click(); method works also correclty. It open a NEW window because Javascript code linked to click() method do following action

var e = window.open("","pdf_view");

When program has run, I obtain following result

enter image description here

I have painted a red circle around PDF button in first Window.

My problem is that I need to continue to click on another PDF button contained in new Window to initiate PDF download.

How can I access it using wv WebView2 variable ?

In tasks manager, I can see that new Windows is attached to Extract-PDF-From-Web application that is the name of my VB.Net application.

enter image description here

schlebe
  • 3,387
  • 5
  • 37
  • 50
  • 1
    [e.NewWindow = (CoreWebView2)sender still results in a separate instance](https://stackoverflow.com/a/68790332/7444103) (read the notes in code) – Jimi Jan 10 '23 at 07:11
  • this answer can help but I wait an answer for VB.Net not for C# where events are defined with another technique. – schlebe Jan 10 '23 at 07:28

1 Answers1

0

To solve this issue, I have added an event handler in wv.NavigationCompleted event in which I have changed e.NewWindow property.

I have also try to set URI but without success.

The full VB.Net solution that works using Visual Studio 2022 is following

Imports Microsoft.Web.WebView2.Core

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        Call InitializeAsync()
    End Sub

    Async Sub InitializeAsync()
        Await wv.EnsureCoreWebView2Async()
        wv.CoreWebView2.Navigate("https://journal.cinetelerevue.sudinfo.be")
    End Sub

    Public Sub NewWindowRequested(sender As Object, e As CoreWebView2NewWindowRequestedEventArgs)
        e.Handled = True
        Dim cwv As CoreWebView2 = sender
        e.NewWindow = cwv
    End Sub

    Private Sub wv_NavigationCompleted(sender As Object, e As CoreWebView2NavigationCompletedEventArgs) Handles wv.NavigationCompleted
        AddHandler wv.CoreWebView2.NewWindowRequested, AddressOf Me.NewWindowRequested
        Threading.Thread.Sleep(1000)
        Call ClickOnPdfButton()
    End Sub

    Async Sub ClickOnPdfButton()
        Dim sButtonCmd = "document.getElementById('readPdfBtn').click();"
        Dim task = Await wv.ExecuteScriptAsync(sButtonCmd)
    End Sub

End Class

After running this code, I obtain following result

enter image description here

schlebe
  • 3,387
  • 5
  • 37
  • 50