XPath Expression Not Working...

Topics: User Forum
Aug 26, 2010 at 8:39 AM
Edited Aug 26, 2010 at 8:40 AM

Hi all,

I know it may be of my noobness in XPath, but let me ask to make sure, cuz I've googled enough.

I have a website and wanna get the news headings from it: www.farsnews.com (it is Persian)

Using FireBug and FireXpath extensions under firefox and by hand I extract and test multiple Xpath expressions that matches the headings, such as:

  • html/body/div[2]/div[2]/div[2]/div[*]/div[2]/a/div[2]
  • .//*[@class="topnewsinfotitle "]
  • .//div[@class="topnewsinfotitle "]

I also tested these using XPather extension and they seem to work pretty well, but when I get to test them... the SelectNodes returns null!

Any clue or hint?

here is a chunk of the code:

 

listBox2.ResetText();

            HtmlAgilityPack.HtmlWeb w = new HtmlAgilityPack.HtmlWeb();
            HtmlAgilityPack.HtmlDocument doc = w.Load("http://www.farsnews.com");
            HtmlAgilityPack.HtmlNodeCollection nc = doc.DocumentNode.SelectNodes(".//div[@class=\"topnewsinfotitle \"]");
            
            listBox2.Items.Add(nc.Count+" Items selected!");

            foreach (HtmlAgilityPack.HtmlNode node in nc) {
                listBox2.Items.Add(node.InnerText);
            }

 

Thanks.

Aug 28, 2010 at 7:00 AM

nothing?

Aug 30, 2010 at 6:58 PM

I would use

"//div[@class='topnewsinfotitle ']"

I'm not sure is there difference between """ and "'", but it's simpler. I used similar expressions many times and they worked.