Skip to content

Instantly share code, notes, and snippets.

@glafarge
Created February 23, 2018 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glafarge/37fbd379689d5cf02d939569976db1d3 to your computer and use it in GitHub Desktop.
Save glafarge/37fbd379689d5cf02d939569976db1d3 to your computer and use it in GitHub Desktop.
Find element by text content with XPath in Python
<root>
<element>A</element>
<element>B</element>
</root>
import lxml.etree
doc = lxml.etree.parse('data.xml')
print doc.xpath('//element[text()="A"]')[0].text
print doc.xpath('//element[text()="A"]')[0].tag
@glafarge
Copy link
Author

Result will be :

A
element

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment