XPath query to select all href attributes of tag, which 'class' attribute equals specified string

后端 未结 4 1120
时光取名叫无心
时光取名叫无心 2020-12-30 00:20

I don\'t know why following query doesn\'t work:

//a/@href[@class=\'specified_string\']
相关标签:
4条回答
  • 2020-12-30 00:21

    Try it the other way round:

    //a[@class='specified_string']/@href
    

    After all, class is an attribute of the <a> element, not an attribute of the href attribute.

    0 讨论(0)
  • 2020-12-30 00:27

    An attribute cannot have attributes. Only elements can have attributes.

    The original XPath expression:

    //a/@href[@class='specified_string'] 
    

    selects any href attribute of any a element, such that the href attribute has an attribute class whose value is 'specified_string'.

    What you want is:

    //a[@class='specified_string']/@href 
    

    that is: the href attribute of any a element that has class atribute with value 'specified_string'.

    0 讨论(0)
  • 2020-12-30 00:28

    You basically say that you are looking for an attribute named href, whose attribute (this is the error) class should be equal to specified_string.

    But you need to find the attribute href of an element a, whose attribute class is specified_string.

    (ndim's answer overlapped mine)

    0 讨论(0)
  • 2020-12-30 00:46

    There is not class attribute present in anchor tag I have href only. It is identified using //*[@href='value'] but //*a[@href='value'] is not working

    0 讨论(0)
提交回复
热议问题