Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Agility Pack get all input fields

I found some code on the internet that finds all the href tags and changes them to google.com, but how can I tell the code to find all the input fields and put custom text in there?

This is the code I have right now:

HtmlDocument doc = new HtmlDocument();
doc.Load(path);
foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
{
    HtmlAttribute att = link.Attributes["href"];
    att.Value = "http://www.google.com";
}
doc.Save("file.htm");

Please, can someone help me, I can't seem to find any information about this on the internet :(.

like image 501
Yuki Kutsuya Avatar asked Feb 20 '23 01:02

Yuki Kutsuya


1 Answers

Change the XPath selector to //input to select all the input nodes:

foreach (HtmlNode input in doc.DocumentNode.SelectNodes("//input"))
{
    HtmlAttribute att = input.Attributes["value"];
    att.Value = "some text";
}
like image 66
Erwin Avatar answered Feb 28 '23 04:02

Erwin