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

89,119

Solution 1

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.

Solution 2

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'.

Solution 3

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)

Share:
89,119

Related videos on Youtube

l245c4l
Author by

l245c4l

Updated on July 09, 2022

Comments

  • l245c4l
    l245c4l almost 2 years

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

    //a/@href[@class='specified_string']
    
    • kans
      kans about 13 years
      As the class attribute may contain multiple class names separated by spaces, you probably actually want: //a[contains(concat(' ',normalize-space(@class),' '), 'some_class_name')]/@href
    • kmkaplan
      kmkaplan about 12 years
      @singpolyma: Good point. Just a nitpick: it is ' some_class_name ' (with spaces around), not 'some_class_name'.
  • l245c4l
    l245c4l about 14 years
    Yes I think it works, but the problem is that query returns attributes, and I want to retrieve attributes' values. I'm writing it in Java and it returned me NodeList of length 2. When I try to print it out, it just prints two xml headers: <?xml version="1.0" encoding="UTF-8"?><?xml version="1.0" encoding="UTF-8"?> two and there is only two links that query will match, so it seems working. But I want to get values of this hrefs. How to do that?
  • ndim
    ndim about 14 years
    That NodeList should contain the list of Nodes representing the href attributes. So just running getNodeValue() on those nodes should give you the href attributes' values.
  • Skywalker326
    Skywalker326 almost 8 years
    This is not working in my case, although I think it's totally right. My Chrome can use //a[@name= "topic"] to extract the right a elements. But //a[@name= "topic"]/@href returns a list of [, , , , , ] as if there isn't a href attribute. This is the an "a element" as example: <a href="/topic/19778287" name="topic">XXX</a>
  • Skywalker326
    Skywalker326 almost 8 years
    Just to let you guys know that that Xpath actually worked. But Chrome console somehow displays an empty-like list. When I assign that list to a variable and print that variable, everything I want is there. I am leaving my original comment there so other people with same issue can relate.