Search code examples
c#htmlxpathhtml-agility-pack

Selecting Multiple div Nodes with different class with xpath in C#


I want to select div nodes with different class with xpath. what should I do? I wrote this code, But it returns error.

HtmlWeb w = new HtmlWeb();
string SearchResults = "http://www.ask.com/web?q=" + query.querytxt;
var hd = w.Load(SearchResults);
var titles = hd.DocumentNode.SelectNodes("//div[contains(@class='web-result ur tsrc')] && //div[contains(@class='web-result ur tsrc_Wiki-SA '] && //div[contains(@class='web-result ur tsrc_tube youtube-result ']"); 

I need these divs

//div[contains(@class='web-result ur tsrc')]
//div[contains(@class='web-result ur tsrc_Wiki-SA ')]
//div[contains(@class='web-result ur tsrc_tube youtube-result ']

this is the error:

Function 'contains' in '//div[contains(@class='web-result ur tsrc')] && //div[contains(@class='web-result ur tsrc_Wiki-SA '] && //div[contains(@class='web-result ur tsrc_tube youtube-result ']' has an invalid number of arguments.


Solution

  • In xpath contains() function works with , to compare value instead of =. So try as below with correct xpath :-

    //div[contains(@class, 'web-result ur tsrc')]
    //div[contains(@class, 'web-result ur tsrc_Wiki-SA ')]
    //div[contains(@class, 'web-result ur tsrc_tube youtube-result ')]
    

    Hope it helps...:)