Accessing DOM from WebBrowser

我怕爱的太早我们不能终老 提交于 2020-01-20 04:24:25

问题


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.

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.

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.


回答1:


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(); 


来源:https://stackoverflow.com/questions/12950413/accessing-dom-from-webbrowser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!