如何访问在 WinForms 中使用 webbrower 控件的 HTML 文档元素?

发布于 2024-12-20 06:23:46 字数 592 浏览 0 评论 0原文

我正在使用 WinForms C# 3.5 WebBrowser

我试图访问以下 HTML 文档中的 head 元素:

this.webBrowserTest.DocumentText = @”<html>
    <head>
        <title>Test JavaScript WinForms</title> 
        <meta name="description" content="Test WinForms" />        
    </head>
    <body></body>
</html>”

HtmlElementCollection headCollection = webBrowserTest.Document.GetElementsByTagName("HEAD");
HtmlElement head = headCollection[0]

headCollection[0] 被作为 null

传递有人知道出了什么问题吗?

谢谢

I'm using WinForms C# 3.5 WebBrowser

I’m trying to get access to the head element in the follow HTML Document:

this.webBrowserTest.DocumentText = @”<html>
    <head>
        <title>Test JavaScript WinForms</title> 
        <meta name="description" content="Test WinForms" />        
    </head>
    <body></body>
</html>”

HtmlElementCollection headCollection = webBrowserTest.Document.GetElementsByTagName("HEAD");
HtmlElement head = headCollection[0]

headCollection[0] is getting passed as null

Would any one have an idea whats wrong?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

淡淡的优雅 2024-12-27 06:23:46

您获取 head 标记的代码看起来不错。在访问文档之前,请确保文档已完全加载。您可以通过执行以下操作来实现此目的:

// Add a handler for load complete.
webBrowserTest.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentLoadCompleted);

// Wait until load completes.
while (webBrowserTest.ReadyState != WebBrowserReadyState.Complete)
{
    System.Windows.Forms.Application.DoEvents();
}

// On load complete, do stuff.
private void DocumentLoadCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    WebBrowser browser = sender as WebBrowser;
    HtmlElement head = browser.Document.GetElementsByTagName("head")[0];
    // do stuff...
}

Your code to get the head tag looks fine. Make sure the document is fully loaded before accessing it. You can accomplish this by doing something like this:

// Add a handler for load complete.
webBrowserTest.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentLoadCompleted);

// Wait until load completes.
while (webBrowserTest.ReadyState != WebBrowserReadyState.Complete)
{
    System.Windows.Forms.Application.DoEvents();
}

// On load complete, do stuff.
private void DocumentLoadCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    WebBrowser browser = sender as WebBrowser;
    HtmlElement head = browser.Document.GetElementsByTagName("head")[0];
    // do stuff...
}
鱼窥荷 2024-12-27 06:23:46

如果您为 head 元素提供 id="headid" 属性,则可以使用 webBrowserTest.Document.All["headid"]。

You can use webBrowserTest.Document.All["headid"], if you'll provide id="headid" attribute for head element.

鹿! 2024-12-27 06:23:46

你尝试过吗

webBrowserTest.Document.All["HEAD"]; // or head

Have you tried

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