Say I have element tree returned by xquery
categories = tree.xpath(u"//ul[@class='tree-root']/li")
for cat in categories:
catName = cat.find('span/span') # is there any shortcut for this?
As you can see I use find in order to access span elements. The question is there any shortcut to access child elements with known path or find is the only way?
You can use
xpath()on elements the same way as on the tree:Unfortunately, I couldn’t find anything about more direct access to subelements in the docs. They can be iterated over or accessed as list elements, but that’s not very handy. The
Elementclass only provides access to the attributes of the current element.