HtmlAgilityPack SelectSingleNode returns 一个没有 InnerHtml 的 HtmlNode
HtmlAgilityPack SelectSingleNode returns an HtmlNode without InnerHtml
我对 SelectSingleNode 方法有点困惑。
我向它传递了一个简单的 xpath 表达式,并希望获得具有完整内容和所有嵌套节点的节点,但实际上只检索了一个 html 标签,我正在寻找没有任何内部和外部文本的内容,并且节点不包含任何子节点。
Xpath:
//form
这里是 html:
<HTML>
<BODY>
<FORM METHOD="POST" ACTION="https://test.com/action">
<INPUT TYPE="hidden" NAME="attribute1" VALUE="some value"/>
<INPUT TYPE="hidden" NAME="attribute2" VALUE="another value"/>
</FORM>
</BODY>
</HTML>
还有一个方法:
public List<Parameter> CollectFilledInputsFromResponseForm(IRestResponse response, string formXpath)
{
var responseAsHtml = new HtmlDocument();
responseAsHtml.LoadHtml(response.Content);
var formDoc = responseAsHtml.DocumentNode.SelectSingleNode(formXpath);
if (formDoc == null)
throw new Exception(string.Format("No form found for '.{0}' xPath", formXpath));
var formHtml = new HtmlDocument();
formHtml.LoadHtml(formDoc.OuterHtml);
var inputs = formHtml.DocumentNode.SelectNodes("//input");
var parameters = new List<Parameter>();
foreach (var input in inputs)
{
var name = input.GetAttributeValue("name", "Name not found");
var value = input.GetAttributeValue("value", "Value not found");
if (name.Equals("Name not found") || value.Equals("Value not found"))
continue;
parameters.Add(new Parameter(){Name = name,Value = value,Type = ParameterType.GetOrPost});
}
return parameters;
}
请指教
Do HtmlNode.ElementsFlags.Remove("form");
before loading the document
见
我对 SelectSingleNode 方法有点困惑。 我向它传递了一个简单的 xpath 表达式,并希望获得具有完整内容和所有嵌套节点的节点,但实际上只检索了一个 html 标签,我正在寻找没有任何内部和外部文本的内容,并且节点不包含任何子节点。
Xpath:
//form
这里是 html:
<HTML>
<BODY>
<FORM METHOD="POST" ACTION="https://test.com/action">
<INPUT TYPE="hidden" NAME="attribute1" VALUE="some value"/>
<INPUT TYPE="hidden" NAME="attribute2" VALUE="another value"/>
</FORM>
</BODY>
</HTML>
还有一个方法:
public List<Parameter> CollectFilledInputsFromResponseForm(IRestResponse response, string formXpath)
{
var responseAsHtml = new HtmlDocument();
responseAsHtml.LoadHtml(response.Content);
var formDoc = responseAsHtml.DocumentNode.SelectSingleNode(formXpath);
if (formDoc == null)
throw new Exception(string.Format("No form found for '.{0}' xPath", formXpath));
var formHtml = new HtmlDocument();
formHtml.LoadHtml(formDoc.OuterHtml);
var inputs = formHtml.DocumentNode.SelectNodes("//input");
var parameters = new List<Parameter>();
foreach (var input in inputs)
{
var name = input.GetAttributeValue("name", "Name not found");
var value = input.GetAttributeValue("value", "Value not found");
if (name.Equals("Name not found") || value.Equals("Value not found"))
continue;
parameters.Add(new Parameter(){Name = name,Value = value,Type = ParameterType.GetOrPost});
}
return parameters;
}
请指教
Do
HtmlNode.ElementsFlags.Remove("form");
before loading the document
见