WPF 网页浏览器控件与 winforms
我正在创建一个 wpf 应用程序,其中使用网络浏览器控件。无论如何,有时我需要查找 html 元素、调用点击和其他基本功能。
在 winforms webbrowser 控件中,我可以通过执行以下操作来实现此目的:
webBrowser1.Document.GetElementById("someId").SetAttribute("value", "I change the value");
在 wpf webbrowser 控件中,我设法通过执行以下操作来实现相同的操作:
dynamic d = webBrowser1.Document;
var el = d.GetElementById("someId").SetAttribute("value", "I change the value");
我还设法使用动态类型在 wpf webbrowser 控件中调用单击。但有时我也会得到例外。
我如何能够在 wpf webbrowser 控件中查找 html 元素、设置属性并调用点击,而不必使用经常出现异常的动态类型?我想用 wpf webbrowser 控件替换 wpf 应用程序中的 winforms webbrowser 控件。
I am creating a wpf application where I am using a webbrowser control. anyways sometimes I am required to look for html elements, invoke clicks, and other basic functionality.
In winforms webbrowser control I am able to achieve this by doing:
webBrowser1.Document.GetElementById("someId").SetAttribute("value", "I change the value");
In wpf webbrowser control I managed to achieve the same thing by doing:
dynamic d = webBrowser1.Document;
var el = d.GetElementById("someId").SetAttribute("value", "I change the value");
I also managed to invoke a click in the wpf webbrowser control by using the dynamic type. Sometimes I get exeptions though.
How will I be able to look for html elements, set attributes and invoke clicks in a wpf webbrowser control without having to use dynamic types where I often get exceptions? I will like to replace my winforms webbrowser control in my wpf application by a wpf webbrowser control.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用以下命名空间,您可以获取所有元素属性和事件处理程序属性:
在构造函数或 xaml 中设置 LoadComplete 事件:
然后在该方法中创建新的 Web 浏览器文档对象并查看可用属性并创建新事件,如下所示:
Use the following namespace that way you can get to all element properties and eventhandler properties:
in constructor or xaml set your LoadComplete event:
then in that method create your new webbrowser document object and view the available properties and create new events as follows:
我这样做的方法是...
下载要使用 HTTPRequest 呈现的页面的 HTML 文本。使用 HTML 敏捷包在 HTML 文本中注入 java 脚本。如果你想使用 jQuery,那么你必须首先 jQuerify 你的页面,然后将事件与你的 dom 元素绑定。您还可以从脚本内或其他方式调用 C# 函数。
不要乱搞动态类型,因此也不例外。
您还可以使用此 链接。
这个和这可能会有所帮助。
The way I have done this is...
Download HTML text of the page you want to render using HTTPRequest. Inject java script using HTML agility pack in HTML text. If you want to use jQuery then you have to jQuerify your page first and then bind event with your dom elements. You can also call your c# function from within the script and other way around.
No messing around with dynamic types and hence no exception.
You can also suppress script error in your WC using extension method on this link.
This and this may help.