在 C# 中访问 DOM 元素
我使用 mshtml 命名空间来访问 COM 浏览器对象返回的 DOM 元素。
我的代码大致如下
object missing = Type.Missing;
ie.Navigate("http://website.com", ref missing, ref missing, ref missing, ref missing);
mshtml.HTMLDocument theDoc = (mshtml.HTMLDocument)ie.Document;
mshtml.HTMLDivElement myDiv = (mshtml.HTMLDivElement)theDoc.getElementById("divID");
现在,到目前为止,myDiv 是一个 DivElement。
但是,网页本身在“divID”下包含更多 DIV。 即DIV的嵌套有很多层。
但 myDIV 对象的类型为 mshtml.HTMLDivElement,它没有为我提供检索更多 DIVS 作为其子项的功能。
这种情况我能做什么? 假设我有更多的 DIV,我需要以嵌套方式检索?
I am using the mshtml namespace to access DOM elements as returned by a COM Browser object.
My code is roughly as follows
object missing = Type.Missing;
ie.Navigate("http://website.com", ref missing, ref missing, ref missing, ref missing);
mshtml.HTMLDocument theDoc = (mshtml.HTMLDocument)ie.Document;
mshtml.HTMLDivElement myDiv = (mshtml.HTMLDivElement)theDoc.getElementById("divID");
Now, up to this point, myDiv is a DivElement.
But, the webpage itself contains more DIV's under "divID". That is, there are many layers of nested DIVs.
But the myDIV object is of type mshtml.HTMLDivElement, which provides me with no function to retrieve more DIVS as its children.
What can I do in this case? Assuming I have more DIV's I need to retrieve in a nested fashion?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能已经弄清楚了这一点,但以防万一...您应该能够将 myDiv 对象转换为 IHTMLElement 接口,并使用其 'children' 属性来迭代 myDiv 的子级。
You might have already figured this out, but just in case... You should be able to cast myDiv object to IHTMLElement interface and use its 'children' property to iterate myDiv's children.