27

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

//a/@href[@class='specified_string']
l245c4l
  • 4,135
  • 9
  • 35
  • 40
  • 3
    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` – singpolyma Apr 29 '11 at 20:15
  • 1
    @singpolyma: Good point. Just a nitpick: it is `' some_class_name '` (with spaces around), *not* `'some_class_name'`. – kmkaplan Apr 25 '12 at 09:31

4 Answers4

55

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.

ndim
  • 35,870
  • 12
  • 47
  • 57
  • 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: 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? – l245c4l Apr 22 '10 at 14:51
  • 1
    That `NodeList` should contain the list of `Node`s representing the `href` attributes. So just running `getNodeValue()` on those nodes should give you the `href` attributes' values. – ndim Apr 22 '10 at 15:00
  • 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: XXX – Skywalker326 Jul 06 '16 at 01:58
  • 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. – Skywalker326 Jul 06 '16 at 02:35
6

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

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
1

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)

topskip
  • 16,207
  • 15
  • 67
  • 99
0

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

Vladimir Vagaytsev
  • 2,871
  • 9
  • 33
  • 36