C#WPF Web浏览器msHTML-探索DOM-查找元素 [英] C# WPF Webbrowser msHTML - Explore DOM - Find Elements
问题描述
我实际上正在使用WPF和WPF WebBrowser在C#中处理个人项目。我真的需要像以前在javascript或php..etc中一样探索html DOM元素。
I'm actually working on a personal project in C# using WPF and WPF WebBrowser. I really need to explore html DOM Elements as we used to do in javascript or php..etc
在我的MainWindow中,我有这个变量:
In my MainWindow I have this variable :
private mshtml.HTMLDocument mainDocument = new mshtml.HTMLDocument();
在我的web浏览器LoadComplete回调中,我有这个:
In my webBrowser LoadComplete callback I have this :
mainDocument = (mshtml.HTMLDocument) mainBrowser.Document;
好,这很好,它正在工作。
Ok, so this is nice, it's working.
现在,如果我这样做:
mshtml.IHTMLElement elem = mainDocument.getElementById("MY_ID");
它也非常好,可以做elem.innerHTML或类似的东西。
it's also very nice, can do elem.innerHTML or somes stuff like that.
但是我的问题是只有HTMLDocument具有通过ID,标记名..etc来查找元素的方法
BUT my problem is only HTMLDocument have methodes to find elements by ID, by tagnames..etc
我不知道如何在IHTMLElement中查找元素。我尝试了一些类似将IHTMLElement强制转换为IHTMLElement2..etc的操作,但没有任何效果。
I don't know how to find elements in IHTMLElement. I tried some stuff like casting IHTMLElement to IHTMLElement2..etc but nothing have worked.
请问有什么想法。很多人谈论托管Winforms Webbrowser,但是我认为它必须只有使用mshtml才能做到这一点。
Please if you have any ideas. A lot of people talks about hosting winforms webbrowser but I think it must have a way to do that only with mshtml.
非常感谢,
如果需要的话更多信息,请随时问我
Thanks a lot, If you need more information, please feel free to ask me
ps:我是法国人,所以对我的英语水平感到很抱歉
ps : I'm french so I'm sorry about my Engish skills
推荐答案
如果要在Winforms或wpf中解析HTML文档,则可以使用出色的解析器htmlagility包。请参阅下面的链接
http://html-agility-pack.net
If you want to parse HTML document in Winforms or wpf, you can use an excellent parser htmlagility pack. Refer to below link http://html-agility-pack.net
var url = "http://html-agility-pack.net/";
var web = new HtmlWeb();
var doc = web.Load(url);
将其加载到文档中后,您可以获取任何属性,标签等。
After loading it in doc, you can get any attribute, tag, etc.
var value = doc.DocumentNode
.SelectNodes("//td/input")
.First()
.Attributes["value"].Value;
这非常简单,只需稍微研究一下文档即可充分利用它。
It's super easy, just explore the doc a bit and you can make full use of it.
您甚至可以从Web浏览器加载html敏捷包,如下所示
You can load html agility pack even from webbrowser, like below
HtmlAgilityPack.HtmlDocument doc = new
HtmlAgilityPack.HtmlDocument();
doc.Load(webBrowser1.DocumentStream);
或者您可以这样做
HtmlAgilityPack.HtmlDocument doc = new
HtmlAgilityPack.HtmlDocument();
doc.Load(webBrowser1.Document);
谢谢
这篇关于C#WPF Web浏览器msHTML-探索DOM-查找元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!