从我的 WPF WebBrowser 检查元素,使用 "inspect element(s)"(IE,Chrome,Firefox) 获取 css 路径 ("copy css-path")
Inspect element from my WPF WebBrowser, using "inspect element(s)"(IE,Chrome,Firefox) to get the css path("copy css-path")
我需要来自网页的正确 css 路径,它将显示在我的 WPF 应用程序中(以便稍后与 HTMLAgilityPack and/or Fizzler 一起使用)。
类似于this,到"copy css-path"或xpath(FireBug),应该是最终目标。
我还发现了一些有趣的 post 比如:
- Whosebug
- Whosebug
- Whosebug
- codeproject
他们都使用 "webBrowser.Document.GetElementFromPoint"、"webBrowser.GetElementFromPoint" 或 "PointToClient",但我在任何图书馆都找不到它,甚至在 msdn.microsoft(WebBrowser Class)
上也找不到
顺便说一下,我使用 visual studio 2010 工具箱中的 "WebBrowserControl" 来显示我的网页。
然后我在客户端找到了一些功能,但这不可能是他们想要的东西(javascript)...
更新时间:6 月 12 日,08:15 下午
这其实很不错post by "dick":
private void myBrowser_LoadCompleted(object sender, NavigationEventArgs e)
{
dynamic doc = myBrowser.Document;
dynamic htmlText = doc.documentElement.InnerHtml;
string htmlstring = htmlText;
}
这有效,但这与我的 WPF 元素无关。
我只得到 html 而不是相应的元素。
我可以在那个 html 文档中搜索文本,但如果它出现不止一次,它就不再是唯一的了。
更新:6 月 13 日,07:15 a.m
根据 this 教程,我找到了解决方法。
我在我的“.xaml”中包含了这个命名空间:
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
然后我添加了旧的 winforms 浏览器:
<WindowsFormsHost Name="wfhSample" Margin="372,12,12,205" MouseLeftButtonDown="wfhSample_MouseLeftButtonDown">
<WindowsFormsHost.Child>
<wf:WebBrowser />
</WindowsFormsHost.Child>
</WindowsFormsHost>
添加到我的"MainWindow()"一个起始值:
MainWindow(){
(wfhSample.Child as System.Windows.Forms.WebBrowser).Navigate("http://www.google.de/");
}
实际部分,获取元素:
/*
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;
}
现在剩下的唯一问题是事件处理程序没有触发(我在属性 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");
}
为什么我不能覆盖事件处理程序(甚至不能覆盖右键)?
更新:6 月 13 日,07:45 a.m
This 可能是一篇有用的文章,我会检查一下。
更新:6 月 16 日,07:00 a.m
现在我找到了一个解决方案,我可以在其中处理 wpf-webbrowser 的事件,但不能处理旧的 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
}
This 帮助了我,我将我的 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"(实际上,从我的角度来看,它不应该为空),另一方面,对于我的新(wpf)网络浏览器控件,我无法访问 "GetElementFromPoint()" 因为它似乎不存在于 wpf 网络浏览器控件中。
更新:6 月 17 日,10:00a.m
看来,
(HTMLDocumentEvents2_Event)(wfhSample.Child as System.Windows.Forms.WebBrowser).Document.DomDocument;
里面的 "wfhSample_ChildChanged" 还没有准备好,因为当我输入相同的语句时:
(HTMLDocumentEvents2_Event)(wfhSample.Child as System.Windows.Forms.WebBrowser).Document.DomDocument;
到 "NewClickEventHandler(mshtml.IHTMLEventObj e)" 里面有所需的数据(但这是错误的事件处理程序)。
所以,现在我需要为我的旧 wpf 浏览器找到 correct/proper 事件处理程序。
更新:6 月 17 日,11:30a.m
好的,现在我找到了基于this文章的解决方案,我将代码更改为:
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 restult = refHtmlElement.TagName;"returns一个"NullReferenceException",坐标好像有问题
只要我有解决方案,我就会 post 或者如果您有想法也可以提供帮助:)
好的,我找到了一个解决方案,包括将 winform webbrowser 纳入我的 wpf 应用程序(看看我关于进程和资源的问题):
1.The .xaml 部分(命名空间)(more details):
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
2.The .xaml 部分(WebBrowser)(more details):
<WindowsFormsHost Name="wfhSample" Margin="372,12,12,205" Loaded="wfhSample_Loaded">
<WindowsFormsHost.Child>
<wf:WebBrowser />
</WindowsFormsHost.Child>
</WindowsFormsHost>
3.The 文件隐藏代码(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-Path 或 XPath,您可以使用 Fizzler、HTMLAgilityPack(this).
有时会出现一些问题(调整大小 window、更改选项卡),事件处理程序不再触发。
我需要来自网页的正确 css 路径,它将显示在我的 WPF 应用程序中(以便稍后与 HTMLAgilityPack and/or Fizzler 一起使用)。
类似于this,到"copy css-path"或xpath(FireBug),应该是最终目标。
我还发现了一些有趣的 post 比如:
- Whosebug
- Whosebug
- Whosebug
- codeproject
他们都使用 "webBrowser.Document.GetElementFromPoint"、"webBrowser.GetElementFromPoint" 或 "PointToClient",但我在任何图书馆都找不到它,甚至在 msdn.microsoft(WebBrowser Class)
上也找不到顺便说一下,我使用 visual studio 2010 工具箱中的 "WebBrowserControl" 来显示我的网页。
然后我在客户端找到了一些功能,但这不可能是他们想要的东西(javascript)...
更新时间:6 月 12 日,08:15 下午
这其实很不错post by "dick":
private void myBrowser_LoadCompleted(object sender, NavigationEventArgs e)
{
dynamic doc = myBrowser.Document;
dynamic htmlText = doc.documentElement.InnerHtml;
string htmlstring = htmlText;
}
这有效,但这与我的 WPF 元素无关。 我只得到 html 而不是相应的元素。 我可以在那个 html 文档中搜索文本,但如果它出现不止一次,它就不再是唯一的了。
更新:6 月 13 日,07:15 a.m
根据 this 教程,我找到了解决方法。
我在我的“.xaml”中包含了这个命名空间:
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
然后我添加了旧的 winforms 浏览器:
<WindowsFormsHost Name="wfhSample" Margin="372,12,12,205" MouseLeftButtonDown="wfhSample_MouseLeftButtonDown">
<WindowsFormsHost.Child>
<wf:WebBrowser />
</WindowsFormsHost.Child>
</WindowsFormsHost>
添加到我的"MainWindow()"一个起始值:
MainWindow(){
(wfhSample.Child as System.Windows.Forms.WebBrowser).Navigate("http://www.google.de/");
}
实际部分,获取元素:
/*
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;
}
现在剩下的唯一问题是事件处理程序没有触发(我在属性 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");
}
为什么我不能覆盖事件处理程序(甚至不能覆盖右键)?
更新:6 月 13 日,07:45 a.m
This 可能是一篇有用的文章,我会检查一下。
更新:6 月 16 日,07:00 a.m
现在我找到了一个解决方案,我可以在其中处理 wpf-webbrowser 的事件,但不能处理旧的 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
}
This 帮助了我,我将我的 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"(实际上,从我的角度来看,它不应该为空),另一方面,对于我的新(wpf)网络浏览器控件,我无法访问 "GetElementFromPoint()" 因为它似乎不存在于 wpf 网络浏览器控件中。
更新:6 月 17 日,10:00a.m
看来,
(HTMLDocumentEvents2_Event)(wfhSample.Child as System.Windows.Forms.WebBrowser).Document.DomDocument;
里面的 "wfhSample_ChildChanged" 还没有准备好,因为当我输入相同的语句时:
(HTMLDocumentEvents2_Event)(wfhSample.Child as System.Windows.Forms.WebBrowser).Document.DomDocument;
到 "NewClickEventHandler(mshtml.IHTMLEventObj e)" 里面有所需的数据(但这是错误的事件处理程序)。
所以,现在我需要为我的旧 wpf 浏览器找到 correct/proper 事件处理程序。
更新:6 月 17 日,11:30a.m
好的,现在我找到了基于this文章的解决方案,我将代码更改为:
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 restult = refHtmlElement.TagName;"returns一个"NullReferenceException",坐标好像有问题
只要我有解决方案,我就会 post 或者如果您有想法也可以提供帮助:)
好的,我找到了一个解决方案,包括将 winform webbrowser 纳入我的 wpf 应用程序(看看我关于进程和资源的问题):
1.The .xaml 部分(命名空间)(more details):
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
2.The .xaml 部分(WebBrowser)(more details):
<WindowsFormsHost Name="wfhSample" Margin="372,12,12,205" Loaded="wfhSample_Loaded">
<WindowsFormsHost.Child>
<wf:WebBrowser />
</WindowsFormsHost.Child>
</WindowsFormsHost>
3.The 文件隐藏代码(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-Path 或 XPath,您可以使用 Fizzler、HTMLAgilityPack(this).
有时会出现一些问题(调整大小 window、更改选项卡),事件处理程序不再触发。