C# 从我的WPF WebBrowser中检查元件,使用;“检查元件”;(即,Chrome、Firefox)获取css路径(“复制css路径”)

C# 从我的WPF WebBrowser中检查元件,使用;“检查元件”;(即,Chrome、Firefox)获取css路径(“复制css路径”),c#,wpf,.net-4.0,webbrowser-control,C#,Wpf,.net 4.0,Webbrowser Control,我需要一个网页中正确的css路径,该路径将显示在我的WPF应用程序中(稍后与HTMLAgilityPack和/或Fizzler一起使用) 最终目标应该是“复制css路径”或xpath(FireBug) 我还发现了一些有趣的帖子,比如: 它们都使用“webBrowser.Document.GetElementFromPoint”、“webBrowser.GetElementFromPoint”或“PointToClient”,但我在任何库中都找不到它,甚至在网上也找不到 顺便说一下,我

我需要一个网页中正确的css路径,该路径将显示在我的WPF应用程序中(稍后与HTMLAgilityPack和/或Fizzler一起使用)

最终目标应该是“复制css路径”或xpath(FireBug)

我还发现了一些有趣的帖子,比如:

它们都使用“webBrowser.Document.GetElementFromPoint”、“webBrowser.GetElementFromPoint”或“PointToClient”,但我在任何库中都找不到它,甚至在网上也找不到

顺便说一下,我使用VisualStudio2010工具箱中的“WebBrowserControl”来显示我的网页

然后我在客户端找到了一些函数,但这不是他们的意思()

更新日期:6月12日下午8:15

这实际上是一个很好的例子:

这是可行的,但这与我的WPF元素无关。 我只得到html,但没有相应的元素。 我可以在html文档中搜索文本,但是如果它出现多次,它就不再是唯一的了

更新日期:6月13日上午7:15

根据教程,我找到了一个解决方法

我将此命名空间包括在我的“.xaml”中:

然后,我在旧的winforms浏览器中添加了:

<WindowsFormsHost Name="wfhSample" Margin="372,12,12,205" MouseLeftButtonDown="wfhSample_MouseLeftButtonDown">
    <WindowsFormsHost.Child>
        <wf:WebBrowser />
    </WindowsFormsHost.Child>
</WindowsFormsHost>
实际零件,以获取元素:

 /*
    System.Drawing.Point point = System.Windows.Forms.Control.MousePosition;
    ...myMethod(..., (wfhSample.Child as System.Windows.Forms.WebBrowser), point.X, point.Y);
    */
     ...myMethod(...,System.Windows.Forms.WebBrowser refWebBrowser2,Int32 valScreenX, Int32 valScreenY,....){

    Point refPoint = refWebBrowser2.PointToClient(new Point(valScreenX, valScreenY));

    System.Windows.Forms.HtmlElement refHtmlElement = refWebBrowser2.Document.GetElementFromPoint(refPoint); //System.Drawing()

    return refHtmlElement.TagName;
    }
现在剩下的唯一问题是事件处理程序没有启动(我在properties窗口上添加了它们):

为什么我不能覆盖事件处理程序(甚至不能覆盖正确的按钮)

更新日期:6月13日上午7:45

可能是一篇有用的文章,我会看看这个

更新日期:6月16日上午7:00

现在我找到了一个解决方案,可以为wpf webbrowser处理事件,但不能为旧的winforms浏览器处理事件

    //new WPF webbrowser  
      private void browser_LoadCompleted(object sender, NavigationEventArgs e)
            {
                mshtml.HTMLDocument doc;
                doc = (mshtml.HTMLDocument)browser.Document;
                mshtml.HTMLDocumentEvents2_Event iEvent;
                iEvent = (mshtml.HTMLDocumentEvents2_Event)doc;
                iEvent.onclick += new mshtml.HTMLDocumentEvents2_onclickEventHandler(NewClickEventHandler);
            }

             private bool NewClickEventHandler(mshtml.IHTMLEventObj e)
            {
            //the click event handler works, but I have no access to "GetElementFromPoint"
 // it does not exist
                        //Point refPoint = browser.PointToClient(new Point(valScreenX, valScreenY));
                //System.Windows.Forms.HtmlElement refHtmlElement = browser.Document.GetElementFromPoint(refPoint);
                //return refHtmlElement.TagName;
                //because the new wpf control does not support for example 'GetElementFromPoint()'
            // I need to get the html controls conntected to the wpf cursor
            }
帮助我和我将wpf应用程序中Winforms webbrowser的事件处理更改为:

//old Winforms webbrowser
        private void wfhSample_ChildChanged(object sender, System.Windows.Forms.Integration.ChildChangedEventArgs e)
        {
        //registering the event here, 
        //calling "OldClickEventHandler" does not work I get a "NullReferenceException" for "_docEvent"
         HTMLDocumentEvents2_Event _docEvent= (HTMLDocumentEvents2_Event)(wfhSample.Child as System.Windows.Forms.WebBrowser).Document.DomDocument;
            _docEvent.onclick += new HTMLDocumentEvents2_onclickEventHandler(OldClickEventHandler);
        }
                    private bool OldClickEventHandler(mshtml.IHTMLEventObj e)
            {
            }
目前,我仍然没有从我在WPF应用程序中选择的webbrowser中获取元素,一方面,我对旧webforms webbrowser的事件有问题,在那里我得到了一个“NullReferenceException”(从我的角度来看,实际上它不应该为null),另一方面,对于我的新webbrowser(WPF)webbrowser控件我无法访问“GetElementFromPoint()”,因为wpf webbrowser控件似乎不存在该控件

更新日期:6月17日上午10:00

看来,

(HTMLDocumentEvents2_Event)(wfhSample.Child as System.Windows.Forms.WebBrowser).Document.DomDocument;
“wfhSample_childchange”中的内容尚未准备好,因为当我放入相同的语句时:

(HTMLDocumentEvents2_Event)(wfhSample.Child as System.Windows.Forms.WebBrowser).Document.DomDocument;
“NewClickEventHandler(mshtml.IHTMLEventObj e)”中包含所需的数据(,但这是错误的事件处理程序)

因此,现在我需要为我的旧wpf浏览器找到正确/适当的事件处理程序

更新日期:6月17日上午11:30

好的,现在我找到了一个基于文章的解决方案,我将代码更改为:

   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; //this is what I need for the first time,to continue
        return true;
    }
以及旧控制工程的事件处理:)

但是“var result=refHtmlElement.TagName;”返回“NullReferenceException”,坐标似乎有问题


一旦我有了一个解决方案,我就会发布它,或者如果你有想法,你也可以提供帮助:)

好的,我找到了一个解决方案,在我的wpf应用程序中包含winform webbrowser(请看一下我关于以下方面的问题:

1.xaml部分(名称空间)():

xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
<WindowsFormsHost Name="wfhSample" Margin="372,12,12,205" Loaded="wfhSample_Loaded">
    <WindowsFormsHost.Child>
        <wf:WebBrowser />
    </WindowsFormsHost.Child>
</WindowsFormsHost>
   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;
    }
2.xaml部分(WebBrowser)():

xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
<WindowsFormsHost Name="wfhSample" Margin="372,12,12,205" Loaded="wfhSample_Loaded">
    <WindowsFormsHost.Child>
        <wf:WebBrowser />
    </WindowsFormsHost.Child>
</WindowsFormsHost>
   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.要获得完整的CSS路径或XPath,可以使用Fizzler、HTMLAgilityPack()。

有时会出现一些小问题(调整窗口大小、更改选项卡),事件处理程序不再启动

   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;
    }