import xml.etree.ElementTree as et
root = et.parse(filename)
result = ''
for e in root.findall('.//child/grandchild'):
    if e.attrib.get('name') == 'foo':
        result = e.text
break
XPath Query in XML using Python