locked
Need help to run JavaScript in WebVeiw2 RRS feed

  • Question

  • Hello:

    Quite some time ago, I used WebView in XML project, I can use WebView to inject JavaScript, run it and get string result in C#.

    But now, with the new WebVew2 coming, I want to find a good example to inject a custom JavaScript and return the string result, but I can’t find a good example.

    For example:

    I created one Windows Forms .NET Framework App project (target .net framework 4.8), then downloaded Edge Canary for Windows 10, install WebView2 nuget package:

    Install-Package Microsoft.Web.WebView2 -Version 1.0.664.37; then from Toolbox. Drag and drop the WebView2 control, and changed its source to https://www.bing.com/

    Now, in Edge Canary, when I browsed to https://www.bing.com/; I can open developer tools, and under “console” tab, run a small JavaScript:

    var divs = document.getElementsByTagName('div');

    var div_length = divs.length;

    console.log(div_length);

    I can run this, and saw the output: 189

    My question is: how I can run such small JavaScript in one WebView2 program to see the result. I wrote some code to do the similar thing on many different web sites, I can only see the result is null.

    Please someone show me some code on how to do this.

    By the way, my IDE is: Visual Studio 2019 Version 16.8.3 on Windows 10 (Version 20H2 OS build 19042.685)

    Tuesday, December 29, 2020 8:52 PM

All replies

  • zydjohn,

    Sorry but you have posted to a forum that deals exclusively with questions/issues about customizing and programming Microsoft Project, a project management application. I suggest you re-post to the following:

    https://docs.microsoft.com/en-us/answers/products/vs

    John

    • Proposed as answer by KHURRAM RAHIM Wednesday, December 30, 2020 1:50 AM
    Tuesday, December 29, 2020 10:50 PM