Equivalent method of WebBrowser.DocumentText in WebView2

Wael Sedky 61 Reputation points
2023-04-29T18:54:03.9966667+00:00

The question is not new, however none of the tons of answers I tried worked for me. Here is what I used to do before in IE WebBrowser object:

int_start_tag = myWebBrowser.DocumentText.IndexOf("radius:\">");

Looks simple enough. I can't figure out how to do the same thing using WebView2. The commented lines are tests and don't work for me. MessageBox.Show is just for me to verify the contents, but I would be doing some processing on the return data.

        private async void button3_Click(object sender, EventArgs e)
        {
            //String html = await webView21.ExecuteScriptAsync("document.documentElement.outerHTML");
            String html = await webView21.CoreWebView2.ExecuteScriptAsync("document.body.outerHTML");
            //String htmldecoded = System.Web.Helpers.Json.Decode(html);
            MessageBox.Show(html);
        }

Developer technologies | Windows Forms
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 90,686 Reputation points
    2023-04-29T20:58:10.1933333+00:00

    This works for me :

                    string sHtml = await webView21.CoreWebView2.ExecuteScriptAsync("document.documentElement.outerHTML");
                    string sHtmlDecoded = System.Text.RegularExpressions.Regex.Unescape(sHtml);
                    Debug.WriteLine(sHtmlDecoded);
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.