HtmlDocument.GetElementById("$id")
我想使用此方法获取带有 $id
的元素,但它匹配一个 meta
标签,其属性值与 $id 相同
。
HtmlDocument是这样的:
<html>
<head>
<meta name="description" content="">
</head>
<body>
<div id="description"></div>
</body>
</html>
我尝试获取 ID 为“description”的标签 div
:
HtmlElement elem = doc.GetElementById("description");
但我得到的是 meta
而不是 div
。为什么 meta
标签匹配?
最佳答案
为什么? Here is an official reference from Microsoft: getElementById 方法:返回对具有指定 ID 或 NAME 属性值的第一个对象的引用。
解决方案:您应该避免在 body 中使用名称属性,这样如果您使用 myHtmlDocument.Body.All[id] 公式(如 sharique ansari 提到的),您就可以通过 id 引用标签。
干杯
关于C# 为什么方法 HtmlDocument.GetElementById 匹配标签的属性名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29137327/