在 C# 中访问 DOM 元素

发布于 2024-07-26 16:14:04 字数 568 浏览 3 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叹梦 2024-08-02 16:14:04

您可能已经弄清楚了这一点,但以防万一...您应该能够将 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文