我正在使用 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
有人知道哪里出了问题吗?
谢谢
最佳答案
您获取 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...
}
关于c# - 如何使用 WinForms 中的 Web 浏览器控件访问 HTML 文档的 <HEAD> 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8416052/