locked
[UWP]WebView html RRS feed

  • Question

  • I am developing a UWP to control the Edge browser with Visual Basic.

    I am using the WebView class with Visual Studio 2017 on Windows 10

    I can open and display a webpage with WebView.

    How can I manipulate the HTML data in the page?

    In particular, how do I get the HTML elements by tag name for example?

    The code that does that in Visual Basic with Internet Explorer is something like:

    Dim wb As New SHDocVw.InternetExplorer Dim HTMLDoc As mshtml.HTMLDocument Dim iHTMLCol As mshtml.IHTMLElementCollection Dim iHTMLEle As mshtml.IHTMLElement wb.Navigate("http://www.google.com") While wb.Busy Or wb.ReadyState <> SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE Threading.Thread.Sleep(100) End While HTMLDoc = wb.Document iHTMLCol = HTMLDoc.getElementsByTagName("input") For Each iHTMLEle In iHTMLCol If Not iHTMLEle.getAttribute("name") Is Nothing Then If iHTMLEle.getAttribute("name").ToString = "xxx" Then iHTMLEle.click() End If Next I have read about WebView1.InvokeScriptAsync but don't know how to implement it.

    Thank you



    Wednesday, March 6, 2019 10:36 AM

Answers

  • Hi,

    So you want to inject JS script to WebView to get the element inside it, right?

    It's not difficult to use WebView.InvokeScriptAsync(String, IIterable<String>) Method, you need to pass the JS code to the method.

    Please refer this following code:

    //I call it in webview's load completed event
    private async void WebView1_LoadCompleted(object sender, NavigationEventArgs e)
            {
    
                Debug.WriteLine("Inject JavaScript");
                // functionString  is the JS function code that you used to get element. It depends on your requirement and you need to write it yourself.
                await webView1.InvokeScriptAsync("eval", new string[] { functionString });
            }
    For more information, please refer this:Interacting with WebView content

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by AntSalc Thursday, March 7, 2019 12:35 PM
    Thursday, March 7, 2019 2:20 AM

All replies

  • Hi,

    So you want to inject JS script to WebView to get the element inside it, right?

    It's not difficult to use WebView.InvokeScriptAsync(String, IIterable<String>) Method, you need to pass the JS code to the method.

    Please refer this following code:

    //I call it in webview's load completed event
    private async void WebView1_LoadCompleted(object sender, NavigationEventArgs e)
            {
    
                Debug.WriteLine("Inject JavaScript");
                // functionString  is the JS function code that you used to get element. It depends on your requirement and you need to write it yourself.
                await webView1.InvokeScriptAsync("eval", new string[] { functionString });
            }
    For more information, please refer this:Interacting with WebView content

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by AntSalc Thursday, March 7, 2019 12:35 PM
    Thursday, March 7, 2019 2:20 AM
  • Hello Roy,

    Thank you for your input. It has been very helpful and I can now make it work.

    Regards,

    Antonio

    Thursday, March 7, 2019 12:37 PM