Click here to Skip to main content
15,860,972 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am building one scraping software. I have loaded the web page using HTMlAgilitypack.

Now that I have to select Elements using the xpath expression. I have build the expression
"//html/body/div[5]/div/div/div[2]/div[0]/div/div[1]/div[0]".This is not working.

When I tried using
doc.DocumentNode.SelectNodes("html") => working // even ("body") => working
but if I use
doc.DocumentNode.SelectNodes("html/body")=> not working


Does any body know how to identify elements using the above expression through HtmlAgilityPack.I have tried my best in searching the internet, but there is no optimal solution for this. In my case the Xpath varies every time. So I am looking for something that would give me the element given the Xpath.

Thanks for your reply.
Posted
Updated 19-Jul-20 11:04am
v2

1 solution

check below results
C#
var html = "\r\n<html>\r\n<body>\r\n\r\n<p>This is a paragraph.</p>\r\n<p>This is a para" +
"graph.</p>\r\n<p>This is a paragraph.</p>\r\n\r\n</body>\r\n</html>\r\n";
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
var t1 = doc.DocumentNode.SelectNodes("html"); //working
var t2 = doc.DocumentNode.SelectNodes("body"); //Not working
var t3 = doc.DocumentNode.SelectNodes("html[1]/body"); //working
var t4 = doc.DocumentNode.SelectNodes("html/body"); //working
var t5 = doc.DocumentNode.SelectNodes("//body"); //working

here doc.DocumentNode.SelectNodes("body"); is not giving any results because there is no body node in document node level. but you can use //body xpath to get node anywhere in the document.
 
Share this answer
 
Comments
Prasaad SJ 16-Jun-14 22:36pm    
Hi Damith,
Do you know any method to access Xpath like "html/body". This returns null in HtmlAgilityPack.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900