The xpath() function in lxml normally returns a list of elements.
If I have an XPath which I expect to return exactly one element, what's the nicest way to:
I'm really looking for the analogue of SQLAlchemy's one() function.
try:
(element,) = tree.xpath('//xpath/selector')
except ValueError:
raise InvalidSelector()
# happened because the list was either empty or contained multiple elements
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With