How to GetElementsByTagName WebView2 winForms ?

vmars316 621 Reputation points
2020-10-21T20:57:43.78+00:00

Tia ;

With WebBrowser1 this works fine 
   var linkElements = this.WebBrowser1.Document.GetElementsByTagName("A");

But this doesn't work for webView2
   var linkElements = this.webView.Document.GetElementsByTagName("A");

Here is what I want to do
            foreach (HtmlElement link in linkElements)
            {
                if (link.GetAttribute("target") == "_blank")
                {
                    link.SetAttribute("target", "_self");
                }
            }

Pls , how should it be coded ?
Tia

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,884 questions
{count} votes

Accepted answer
  1. Tianyu Sun-MSFT 30,336 Reputation points Microsoft Vendor
    2020-10-22T09:13:48.213+00:00

    Hello Vernon,

    Thank you for taking time to post this issue in Microsoft Q&A forum.

    I checked the class of WebView2 and found that WebView2 does not include a method which is equivalent to GetElementsByTagName() method to meet your requirement.

    For your requirements, here is a similar issue: Coming from Webbrowser, Winforms DOM Document Access in WebView2… which may be helpful.

    Sincerely,
    Tianyu

    • If the answer is helpful, please click "Accept Answer" and upvote it.
      Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    0 comments No comments

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.