Manage WPF web browser and winforms

I am creating a wpf application where I use a webbrowser control. in any case, I sometimes have to look for html elements, call clicks and other basic functions.

In winbrowser web browser control, I can achieve this by doing:

 webBrowser1.Document.GetElementById("someId").SetAttribute("value", "I change the value");

In the wpf control of the web browser, I managed to achieve the same:

  dynamic d = webBrowser1.Document;  
  var el = d.GetElementById("someId").SetAttribute("value", "I change the value");

I also managed to trigger a click in the wpf web control using the dynamic type. Sometimes I get exceptions.

How can I search for html elements , set attributes and trigger clicks in a wpf web control without using dynamic types, where do I often get exceptions? I want to replace the webbrowser winforms control in a wpf application using the wpf web browser control.

+5
source share
2 answers

How I did it ...

Download the HTML text of the page you want to render using HTTPRequest. Introduce java script using HTML flexibility package in HTML text. If you want to use jQuery, you first need to jQuerify your page and then bind the event to your dom elements. You can also call your C # function from a script and vice versa. Do not interfere with dynamic types and, therefore, are no exception.

You can also suppress the script error in your WC using the extension method on this link .

This and this can help.

-3
source

, :

    using mshtml;

    private mshtml.HTMLDocumentEvents2_Event documentEvents;
    private mshtml.IHTMLDocument2 documentText;

xaml LoadComplete:

    webBrowser.LoadCompleted += webBrowser_LoadCompleted;

webbrowser :

    private void webBrowser_LoadCompleted(object sender, NavigationEventArgs e)
    {
        documentText = (IHTMLDocument2)webBrowserChat.Document; //this will access the document properties as needed
        documentEvents = (HTMLDocumentEvents2_Event)webBrowserChat.Document; // this will access the events properties as needed
        documentEvents.onkeydown += webBrowserChat_MouseDown;
        documentEvents.oncontextmenu += webBrowserChat_ContextMenuOpening;
    }

    private void webBrowser_MouseDown(IHTMLEventObj pEvtObj)
    {
         pEvtObj.returnValue = false; // Stops key down
         pEvtObj.returnValue = true; // Return value as pressed to be true;
    }

    private bool webBrowserChat_ContextMenuOpening(IHTMLEventObj pEvtObj)
    {
        return false; // ContextMenu wont open
        // return true;  ContextMenu will open
        // Here you can create your custom contextmenu or whatever you want
    }
+1

All Articles