Equivalent of WebBrowser.Document in WebView2

Priyanka Songra 6 Reputation points
2022-03-29T17:55:07.76+00:00

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.

Thanks,
Priyanka

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

2 answers

Sort by: Most helpful
  1. Castorix31 84,546 Reputation points
    2022-03-29T18:22:10.773+00:00

    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
    2022-04-12T18:24:57.88+00:00

    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.

    Thanks,
    Priyanka

    0 comments No comments

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.