1

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.

viraptor
  • 33,322
  • 10
  • 107
  • 191

1 Answers1

2

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.

emboss
  • 38,880
  • 7
  • 101
  • 108