Inspect element from my WPF WebBrowser, using “inspect element(s)”(IE,Chrome,Firefox) to get the css path(“copy css-path”)

前端 未结 1 1270
执笔经年
执笔经年 2021-01-21 20:33

I need the correct css path from a webpage, which will be displayed in my WPF application(to use it later with HTMLAgilityPack and/or Fizzler).

Something like this, to \

相关标签:
1条回答
  • 2021-01-21 20:49

    Ok,I found a solution including the winform webbrowser into my wpf application(take a look at my question regarding, processes and resources):

    1.The .xaml part(namespace)(more details):

    xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
    

    2.The .xaml part(WebBrowser)(more details):

    <WindowsFormsHost Name="wfhSample" Margin="372,12,12,205" Loaded="wfhSample_Loaded">
        <WindowsFormsHost.Child>
            <wf:WebBrowser />
        </WindowsFormsHost.Child>
    </WindowsFormsHost>
    

    3.The code behind file(more details):

       private void wfhSample_Loaded(object sender, RoutedEventArgs e)
        {
            bool complete = false;
            (wfhSample.Child as System.Windows.Forms.WebBrowser).DocumentCompleted += delegate
            {
                if (complete)
                    return;
                complete = true;
                // DocumentCompleted is fired before window.onload and body.onload
                (wfhSample.Child as System.Windows.Forms.WebBrowser).Document.Window.AttachEventHandler("onload", delegate
                {
                    // Defer this to make sure all possible onload event handlers got fired
                    System.Threading.SynchronizationContext.Current.Post(delegate
                    {
                        // try webBrowser1.Document.GetElementById("id") here
                        //System.Windows.MessageBox.Show("window.onload was fired, can access DOM!");
                        var bla = (wfhSample.Child as System.Windows.Forms.WebBrowser).Document.DomDocument;
                        HTMLDocumentEvents2_Event _docEvent = (HTMLDocumentEvents2_Event)(wfhSample.Child as System.Windows.Forms.WebBrowser).Document.DomDocument;
                        _docEvent.onclick += new HTMLDocumentEvents2_onclickEventHandler(OldClickEventHandler);
                    }, null);
                });
            };
    
            (wfhSample.Child as System.Windows.Forms.WebBrowser).Navigate("http://www.example.com");
        }
        private bool OldClickEventHandler(mshtml.IHTMLEventObj e)
        {
            System.Drawing.Point point = System.Windows.Forms.Control.MousePosition;
            System.Drawing.Point refPoint = (wfhSample.Child as System.Windows.Forms.WebBrowser).PointToClient(new System.Drawing.Point(point.X, point.Y));
            System.Windows.Forms.HtmlElement refHtmlElement = (wfhSample.Child as System.Windows.Forms.WebBrowser).Document.GetElementFromPoint(refPoint);
            var restult = refHtmlElement.TagName; 
            return true;
        }
    

    4.To get the full CSS-Path or XPath you could use Fizzler, HTMLAgilityPack(this).

    Sometimes it is a little buggy(resizing window, changing tabs), the event handler does not fire anymore.

    0 讨论(0)
提交回复
热议问题