【问题标题】:Xpath has an invalid tokenXpath 有一个无效的令牌
【发布时间】:2017-05-22 16:11:24
【问题描述】:

我想用 xpath 获取一个带有“ac-algo fz-l ac-21th lh-24”类名的 html 页面中的所有链接。

我写的代码:

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


但我得到这个错误:

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

【问题讨论】:

  • 你能显示包含class属性的html的sn-p吗?

标签: c# xml xpath


【解决方案1】:

用等号替换逗号并删除括号,以按确切的类名匹配元素:

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

或者你可以使用contains()通过部分类名匹配元素:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 2011-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-09
    • 2017-03-09
    相关资源
    最近更新 更多