从web浏览器访问DOM [英] Accessing DOM from WebBrowser

查看:112
本文介绍了从web浏览器访问DOM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个类似浏览器的小应用程序,让我修改浏览web站点的外观(例如做链接的字体更大)。它是专为微软的Surface,要在一个巨大的触摸屏使用。它使用WPF的UI。

I am trying to implement a browser-like little application that would allow me to modify the viewed web-sites appearance (e.g. make the font for links bigger). It is designed for Microsoft Surface, to be used on a huge touchscreen. It uses WPF for the UI.

我打算使用此任务WebBrowser控件。然而,有两个班被称为web浏览器中的文档。其中之一是在 System.Windows.Forms的,另外在 System.Windows.Controls的。第一个可以访问DOM模型,但适用于表单的应用程序(如果我理解正确的话,那绝对不是我有什么)。第二个是默认添加,如果你在XAML中添加控制器,但它给到DOM的访问权限。

I am intending to use a WebBrowser control for this task. However there are two classes called WebBrowser in the docs. One of them is in System.Windows.Forms, the other in System.Windows.Controls. The first one gives access to DOM model, but is intended for Forms applications (if I understand correctly, that's definitely not what I have). The second one is added by default if you add the controller in xaml, but it gives no access to the DOM.

那么,如何从地表web浏览器访问DOM模型?我很新的C#和微软的技术,所以我道歉,如果我的问题是不清楚或明显的。

So, how do I access the DOM model from a WebBrowser for Surface? I am very new to c# and Microsoft technologies, so I apologise if my question is unclear or obvious.

推荐答案

对于 System.Windows.Controls.WebBrowser 类,你可以使用文档属性。
MSHTML 引用您的项目,应可通过项目右键,选择添加引用,那么你应该能够将其转换为mshtml.IHTMLDocument2

For the System.Windows.Controls.WebBrowser class you can use the Document property. Add mshtml reference to your project that should be available by right click on project and selecting Add Reference, then you should be able to cast it to mshtml.IHTMLDocument2

mshtml.IHTMLDocument2 htmlDoc = webBrowser.Document as mshtml.IHTMLDocument2;
// do something like find button and click
htmlDoc.all.item("testBtn").click(); 

这篇关于从web浏览器访问DOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆