question

PriyankaSongra-6960 avatar image
0 Votes"
PriyankaSongra-6960 asked PriyankaSongra-6960 answered

Equivalent of WebBrowser.Document in WebView2

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-embedded
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Castorix31 avatar image
0 Votes"
Castorix31 answered

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)


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

PriyankaSongra-6960 avatar image
0 Votes"
PriyankaSongra-6960 answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.