mary
mary

Reputation: 267

Xpath has an invalid token

I Want to get all links in a html page with "ac-algo fz-l ac-21th lh-24" class name with xpath.

i write thic code :

string links = node.SelectSingleNode(".//a[(@class,'ac-algo fz-l ac-21th lh-24')]").GetAttributeValue("href", null);


but i get this error:

'.//a[(@class,'ac-algo fz-l ac-21th lh-24')]' has an invalid token.

Upvotes: 0

Views: 2631

Answers (1)

Andersson
Andersson

Reputation: 52685

Replace comma with equal sign and remove parenthesis as below to match element by exact class name:

string links = node.SelectSingleNode(".//a[@class='ac-algo fz-l ac-21th lh-24']").GetAttributeValue("href", null);

Or you can use contains() to match element by partial class name:

string links = node.SelectSingleNode(".//a[contains(@class,'ac-algo fz-l ac-21th lh-24')]").GetAttributeValue("href", null);

Upvotes: 2

Related Questions