Equivalent of WebBrowser.Document in WebView2

Priyanka Songra 6 Reputation points

We have a VSTO addin for MS Project 2010/2013/2016/2019, where it imports/exports data from an external application and these pages are rendered in embedded Internet Explorer browser from MS Project.
Since IE support will be ending in few years, we are moving to new embedded browser control known as the Microsoft Edge WebView2 control.
I need the handle to html document so that I can get elements from html based on element id or tag name and call click handler on html element.
Need equivalent of :
HtmlDocument doc = browser.Document
HtmlElementCollection elCol = doc.GetElementsByTagName("button");

Any help would be highly appreciated.


Windows for IoT
Windows for IoT
A family of Microsoft operating systems designed for use in Internet of Things (IoT) devices.
369 questions
0 comments No comments
{count} vote

2 answers

Sort by: Most helpful
  1. Castorix31 79,746 Reputation points

    It is done with CoreWebView2.ExecuteScriptAsync
    You can find samples like at Chromium WebView2 Control and .NET to JavaScript Interop - Part 2 (C# but same thing in VB)

    0 comments No comments

  2. Priyanka Songra 6 Reputation points

    Hi @Castorix31 ,
    I tried using
    String html =await webView2Browser.CoreWebView2.ExecuteScriptAsync("document.getElementById('ATTRIBUTE_PAGE_ID').value");
    And then I want to compare this html to some string and perform some action, but my application is crashing after the above statement is executed.
    Also do we need to initialize 'document'?
    Any pointers to resolve this issue.


    0 comments No comments