【发布时间】:2022-01-13 09:43:00
【问题描述】:
我有一个使用 Webview2 浏览器打开表单的模块。然后,Webview2 控件使用用户用户名和密码(他之前在两个文本框中输入)登录网站并通过几个链接加载到用户可以搜索客户的特定网页,www.login.ca/search。在用户输入名称并单击搜索按钮后,下一页会加载一秒钟,然后再次加载 www.login.ca/search 页面。我只是希望用户能够继续浏览该页面,而无需浏览器重新加载“login.ca/search”网页。
我有这个:
Imports System.Windows.Interop
Imports Microsoft.Web.WebView2.Core
Module SearchForCustomer
Public Sub CheckCustomer()
WebviewForm.Show()
OpenAndLogIn()
End Sub
Public Sub OpenAndLogIn()
'Open webpage and log in
Dim Loginwebaddress As String = "https://www.login.ca"
WebviewForm.WebView21.Source = New Uri(Loginwebaddress)
AddHandler(WebviewForm.WebView21.NavigationCompleted), AddressOf FirstPageWebView
End Sub
Private Async Sub FirstPageWebView(ByVal sender As Object, ByVal e As CoreWebView2NavigationCompletedEventArgs)
Dim msg = TextBox1.Text 'Username
Dim msg2 = TextBox2.Text 'password
Await WebviewForm.WebView21.ExecuteScriptAsync($"document.getElementById('LoginUN').value = '{msg}';")
Await WebviewForm.WebView21.ExecuteScriptAsync($"document.getElementById('Login1PW').value = '{msg2}';")
Await WebviewForm.WebView21.ExecuteScriptAsync($"document.getElementById('Login1_DoLogin').click();")
AddHandler(WebviewForm.WebView21.NavigationCompleted), AddressOf LoginWebpageLoaded
End Sub
Private Sub LoginWebpageLoaded
'Load the search for customer page
Dim Loginwebaddress As String = "https://www.login.ca/search"
WebviewForm.WebView21.Source = New Uri(Loginwebaddress)
End Sub
【问题讨论】:
-
我对 vb.net 不熟悉,所以我将其写为评论,但是当您更改源代码时,
AddHandler(WebviewForm.WebView21.NavigationCompleted)可能会经常执行,然后再次重定向您。我认为您可能需要取消注册处理程序或使用不同的操作。如果您已经尝试过,请给我一个标志,然后我删除此评论^^ -
谢谢,有帮助。我不得不删除两个处理程序。谢谢你的好建议。
标签: c# vb.net webview2 addhandler