viraptor
viraptor

Reputation: 34155

Xpath doesn't match

I'm trying to get some elements from a page. Unfortunately it results with an empty list. The pretty-printed tree includes this element:

<html:a title="..." href="..." id="..." class="topic_title">...</html:a>

However when I do this on the same tree:

page.xpath('''.//a[@class="topic_title"]''')

I get an empty list. The tree was created with html5lib / lxml treebuilder.

Upvotes: 1

Views: 167

Answers (1)

emboss
emboss

Reputation: 39620

It seems as if you are dealing with XHTML, so you could register the namespace html before evaluating the XPath expression:

page.xpath('''.//html:a[@class="topic_title"]''',
           namespaces={'html': 'http://www.w3.org/1999/xhtml'})

See also Namespaces and Prefixes:

If your XPath expression uses namespace prefixes, you must define them in a prefix mapping. To this end, pass a dictionary to the namespaces keyword argument that maps the namespace prefixes used in the XPath expression to namespace URIs.

Upvotes: 2

Related Questions