我必须将程序中的webBrowser控件升级到webView2,因为许多页面不再工作了。我有一个返回活动的内部文本的方法。
if (XtraMessageBox.Show(this, "Text:\r\n\r\n" +
webBrowser.Document.ActiveElement.InnerText + "\r\n\r\n" +
"will be added to description",
"DESCRIPTION UPDATE", MessageBoxButtons.YesNo, MessageBoxIcon.Information)
.Equals(DialogResult.No))
return;
descriptionMemoEdit.EditValue += webBrowser.Document.ActiveElement.InnerText;
我想找一个和webView2相当的东西,但什么也找不到。我试过了
webView.ExecuteScriptAsync("document.ActiveElement.InnerText").ToString()
and
webView.CoreWebView2.ExecuteScriptAsync("document.ActiveElement.InnerText").ToString()
但是它只是返回System.Threading.Tasks.Task`1System.String,而不是实际的文本。我看到webView2没有DOM。我怎么才能得到内情呢?
发布于 2022-04-06 08:59:14
你可以这样用它。
await ExecuteScriptAsync("your script here...");
别忘了等待。
编辑:
将webView21_CoreWebView2InitializationCompleted事件添加到webview2中。
在您的事件中添加此示例代码。
private void webView21_CoreWebView2InitializationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs e)
{
this.webView21.CoreWebView2.NavigationCompleted += CoreWebView2_NavigationCompleted;
}
然后添加一些方法。
private async void CoreWebView2_NavigationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationCompletedEventArgs e)
{
await webView21.CoreWebView2.ExecuteScriptAsync("your script here...")
;}
https://stackoverflow.com/questions/71763417
复制相似问题