将 webBrowser 更改为 webView2 c#

发布于 2025-01-19 07:36:36 字数 873 浏览 2 评论 0原文

我必须将程序中的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`1 [System.String],而不是实际文本。 我看到WebView2没有DOM。 我该如何获得Innertext?

I must upgrade webBrowser control in my program to webView2 because many pages are not working anymore.
I have a method that returns the inner text of the activelement

 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;

I am trying to find an equivalent to webView2 but I cant find anything.
I tried

webView.ExecuteScriptAsync("document.ActiveElement.InnerText").ToString()
and 
webView.CoreWebView2.ExecuteScriptAsync("document.ActiveElement.InnerText").ToString()

but it just returns
System.Threading.Tasks.Task`1[System.String] and not the actual text.
I saw that webView2 doesnt have DOM.
How can I get the innertext?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

携君以终年 2025-01-26 07:36:36

你可以像这样使用它。

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...")
    ;}

You can use it like this.

await ExecuteScriptAsync("your script here...");

Don't forget to put await.

EDITED :

Add a webView21_CoreWebView2InitializationCompleted event to your webview2.

On your event add this sample code.

private void webView21_CoreWebView2InitializationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs e)
        {
            
            this.webView21.CoreWebView2.NavigationCompleted += CoreWebView2_NavigationCompleted;

        }

Then add some method.

 private async void CoreWebView2_NavigationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationCompletedEventArgs e)
                {
    await webView21.CoreWebView2.ExecuteScriptAsync("your script here...")
    ;}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文