从我的WPF web浏览器检查的元素,使用"检查元素(S)QUOT;(IE,Chrome浏览器,火狐)得到的CSS路径("复制CSS路径") [英] Inspect element from my WPF WebBrowser, using "inspect element(s)"(IE,Chrome,Firefox) to get the css path("copy css-path")
问题描述
我需要从一个网页,它会显示在我的WPF应用程序的正确的CSS路径(以后使用它与HTMLAgilityPack和/或Fizzler)。
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).
如这个个什么东西>,复制CSS路径或XPath(萤火虫),应是最终目标。
Something like this, to "copy css-path" or xpath(FireBug), should be the final goal.
我也发现了一些有趣的帖子,如:
I also found some interesting post like:
- <一个href="http://stackoverflow.com/questions/4207003/c-sharp-webbrowser-control-how-to-get-data-under-mouse-pointer">stackoverflow
- <一个href="http://stackoverflow.com/questions/2396121/how-can-i-get-text-under-mouse-pointer-webbrowser-control">stackoverflow
- <一个href="http://stackoverflow.com/questions/4219526/html-render-with-inspect-element-functionality-how-to-c-sharp">stackoverflow
- 的 $ C $的CProject
- stackoverflow
- stackoverflow
- stackoverflow
- codeproject
它们都使用webBrowser.Document.GetElementFromPoint,webBrowser.GetElementFromPoint或PointToClient,但我找不到它的任何图书馆,甚至不是<一个href="https://msdn.microsoft.com/en-us/library/system.windows.controls.webbrowser%28v=vs.100%29.aspx"相对=nofollow> msdn.microsoft(web浏览器类)
They all use "webBrowser.Document.GetElementFromPoint", "webBrowser.GetElementFromPoint" or "PointToClient", but I can not find it in any library and not even on msdn.microsoft(WebBrowser Class)
当我使用从Visual Studio 2010工具箱中的WebBrowserControl,以显示我的网页的方式。
By the way I use the "WebBrowserControl" from visual studio 2010 toolbox to display my webpage.
然后我发现在客户端的一些功能,但是这不能成为他们的意思的东西(的 JavaScript的)...
Then I found some function on the client-side, but this can not be the thing they meant(javascript)...
更新:12日止,下午8时15分
这其实是一个很好的<一个href="http://stackoverflow.com/questions/8527036/how-to-read-element-content-from-web-page-using-the-wpf-webbrowser-component">post通过迪克:
private void myBrowser_LoadCompleted(object sender, NavigationEventArgs e)
{
dynamic doc = myBrowser.Document;
dynamic htmlText = doc.documentElement.InnerHtml;
string htmlstring = htmlText;
}
这工作,但这个站在没有关系,我的WPF元素。 我只得到了HTML,但没有相应的元素。 我可以搜索在该HTML文档的文本,但如果它出现一次以上,其不是唯一的了。
This works, but this stands in no relation to my WPF element. I get only the html but not the corresponding element. I could search for the text in that html doc, but if it appears more than once, its is not unique anymore.
更新时间:6月13日,上午07时15
根据本教程中,我发现了一个解决方法。
Based on this tutorial I found a workaround.
我列入这个命名空间在我的的.xaml:
I included this namespace in my ".xaml":
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
然后,我添加了旧的WinForms浏览器:
Then I added the the old winforms browser:
<WindowsFormsHost Name="wfhSample" Margin="372,12,12,205" MouseLeftButtonDown="wfhSample_MouseLeftButtonDown">
<WindowsFormsHost.Child>
<wf:WebBrowser />
</WindowsFormsHost.Child>
</WindowsFormsHost>
添加到我的主窗口()初始值:
Added to my "MainWindow()" a starting value:
MainWindow(){
(wfhSample.Child as System.Windows.Forms.WebBrowser).Navigate("http://www.google.de/");
}
实际的一部分,要让元素:
The actual part, to get the element:
/*
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;
}
唯一的问题,这是现在剩下的就是该事件处理程序不火(我加了他们在属性窗口中):
The only problem,which is left by now is that the event handler does not fire(I added them over the properties window):
//winforms-browser
private void wfhSample_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("old");
}
//wpf-browser
private void browser_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("new");
}
为什么我不能覆盖的事件处理程序(甚至不正确的按钮)?
Why can't I overwrite the event handler(not even the right button) ?
更新时间:6月13日,07:45
<一个href="http://stackoverflow.com/questions/2189510/wpf-webbrowser-mouse-events-not-working-as-expected">This可能是一个有用的文章中,我将检查了这一点。
This could be an useful article, I will check this out.
更新:16日止,上午07:00
现在我找到了一个解决方案,在那里我可以处理事件的WPF的网页浏览器,而不是旧的WinForms浏览器。
Now I found a solution, where I can handle an event for a wpf-webbrowser, but not for the old winforms-browser.
//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
}
<一个href="http://stackoverflow.com/questions/6373853/net-webbrowser-control-get-active-contextmenu">This帮助了我,我在我的WPF应用程序改变了事件处理的WinForms网页浏览器为:
This helped me and I changed the event handling for Winforms webbrowser in my wpf application to:
//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应用程序中选择我有一个事件旧的WebForms的浏览器,在那里我得到一个NullReferenceException异常有问题的网页浏览器元素(实际上它不应该是从我的角度来看空),并在另一方面也为我的新(WPF)WebBrowser控件我无法访问GetElementFromPoint(),因为它似乎不存在了WPF WebBrowser控件。
At the moment I still do not get the element from the webbrowser which I selected in my WPF-application on the one hand I have a problem with an event for the old webforms webbrowser, where I get a "NullReferenceException"(actually it should not be null from my point of view) and on the other hand for my new(wpf) webbrowser control I can not access "GetElementFromPoint()" because it does not seem to exist for the wpf webbrowser control.
更新:17日止,上午10:00
看来,那
(HTMLDocumentEvents2_Event)(wfhSample.Child as System.Windows.Forms.WebBrowser).Document.DomDocument;
这里面的wfhSample_ChildChanged还没有准备好,因为当我把相同的语句:
which is inside "wfhSample_ChildChanged" is not ready yet, because when I put the same statement:
(HTMLDocumentEvents2_Event)(wfhSample.Child as System.Windows.Forms.WebBrowser).Document.DomDocument;
成NewClickEventHandler(mshtml.IHTMLEventObj五)它在它需要的数据(,但这是错误的事件处理程序)。
into "NewClickEventHandler(mshtml.IHTMLEventObj e)" it has the required data in it(,but this is the wrong event handler).
所以,现在我需要找到正确的/合适的事件处理程序,我的老WPF浏览器。
So, now I need to find the correct/proper event handler for my old wpf browser.
更新:17日止,上午11:30
好了,现在我找到了一个解决方案,基于<一href="http://stackoverflow.com/questions/18368778/getting-html-body-content-in-winforms-webbrowser-after-body-onload-event-execute#18370524">this文章中,我改变了我的code到:
Ok, now I found a solution based on this article, I changed my code to:
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;
}
和事件处理的旧的控制工程:)
And the event handling for the old control works :)
但VAR restult = refHtmlElement.TagName;返回NullReferenceException异常,似乎有一些错误的坐标。
But the "var restult = refHtmlElement.TagName;" returns a "NullReferenceException", there seems to be something wrong with the coordinates.
只要我有一个解决方案,我将它张贴,或者如果你有一个想法,你可以很好的帮助:)
As soon as I have a solution I will post it or if you have an idea you can help as well:)
推荐答案
好吧,我发现包括WinForm的web浏览器到我的WPF应用程序(带有关,过程和资源来看看我的问题)的解决方案:
Ok,I found a solution including the winform webbrowser into my wpf application(take a look at my question regarding, processes and resources):
1,部分名为.xaml(空间)(更多详细信息):
1.The .xaml part(namespace)(more details):
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
2,部分名为.xaml(web浏览器)(更多详细信息):
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,$ C $隐藏文件C(<一href="http://stackoverflow.com/questions/18368778/getting-html-body-content-in-winforms-webbrowser-after-body-onload-event-execute#18370524">more详细信息):
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得到充分的CSS路径或XPath你可以使用Fizzler,HTMLAgilityPack(<一href="http://stackoverflow.com/questions/20736331/get-xpath-from-clicked-htmlelement-in-webbrowsercontrol">this).
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.
这篇关于从我的WPF web浏览器检查的元素,使用&QUOT;检查元素(S)QUOT;(IE,Chrome浏览器,火狐)得到的CSS路径(&QUOT;复制CSS路径&QUOT;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!