C# Htmlagilitypack 中的 GetElementsByTagName
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10260255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GetElementsByTagName in Htmlagilitypack
提问by Ali
How do I select an element for e.g. textbox if I don't know its id?
如果我不知道它的 id,如何为例如文本框选择一个元素?
If I know its id then I can simply write:
如果我知道它的 id,那么我可以简单地写:
HtmlAgilityPack.HtmlNode node = doc.GetElementbyId(id);
But I don't know textbox's ID and I can't find GetElementsByTagName method in HtmlagilityPack which is available in webbrowser control. In web browser control I could have simply written:
但是我不知道文本框的 ID,并且在 Webbrowser 控件中可用的 HtmlagilityPack 中找不到 GetElementsByTagName 方法。在 Web 浏览器控件中,我可以简单地编写:
HtmlElementCollection elements = browser[i].Document.GetElementsByTagName("form");
foreach (HtmlElement currentElement in elements)
{
}
EDIT
编辑
Here is the HTML form I am talking about
这是我正在谈论的 HTML 表单
<form id="searchform" method="get" action="/test.php">
<input name="sometext" type="text">
</form>
Please note I don't know the ID of form. And there can be several forms on same page. The only thing I know is "sometext" and I want to get this element using just this name. So I guess I will have to parse all forms one by one and then find this name "sometext" but how do I do that?
请注意,我不知道表单的 ID。并且同一页面上可以有多个表单。我唯一知道的是“sometext”,我想只使用这个名称来获取这个元素。所以我想我将不得不一一解析所有形式,然后找到这个名字“sometext”,但我该怎么做?
采纳答案by jessehouwing
If you're looking for the tag by its tagName (such as formfor <form name="someForm">), then you can use:
如果您要通过 tagName(例如formfor <form name="someForm">)查找标签,则可以使用:
var forms = document.DocumentNode.Descendants("form");
If you're looking for the tag by its name property (such as someFormfor <form name="someForm">, then you can use:
如果您要按名称属性查找标签(例如someFormfor <form name="someForm">,则可以使用:
var forms = document.DocumentNode.Descendants().Where(node => node.Name == "formName");
For the last one you could create a simple extension method:
对于最后一个,您可以创建一个简单的扩展方法:
public static class HtmlNodeExtensions
{
public static IEnumerable<HtmlNode> GetElementsByName(this HtmlNode parent, string name)
{
return parent.Descendants().Where(node => node.Name == name);
}
public static IEnumerable<HtmlNode> GetElementsByTagName(this HtmlNode parent, string name)
{
return parent.Descendants(name);
}
}
Note: You can also use SelectNodesand XPath to query your document:
注意:您还可以使用SelectNodes和 XPath 来查询您的文档:
var nodes = doc.DocumentNode.SelectNodes("//form//input");
Would give you all inputs on the page that are in a form tag.
将为您提供页面上表单标签中的所有输入。
var nodes = doc.DocumentNode.SelectNodes("//form[1]//input");
Would give you all the inputs of the first form on the page
会给你页面上第一个表单的所有输入
回答by L.B
I think you are looking for something like this
我想你正在寻找这样的东西
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml("....");
var inputs = doc.DocumentNode.Descendants("input")
.Where(n => n.Attributes["name"]!=null && n.Attributes["name"].Value == "sometext")
.ToArray();
回答by Adrian Rosca
Any node by name:
按名称的任何节点:
doc.DocumentNode.SelectNodes("//*[@name='name']")
Input nodes by name:
按名称输入节点:
doc.DocumentNode.SelectNodes("//input[@name='name']")

