lxml find element by name, but use variable in search

霸气de小男生 提交于 2019-12-12 03:56:13

问题


I have a problem with the find function in lXML. But i think this is more a generic question how to tell that i want to check against the value, not the object reference.

So here is the code that works:

step = xml_obj.find('.//step/name[text()="Design"]').getparent()

If i try to replace the string with an object, the result is always None.

stepn = 'Design'
step = xml_obj.find('.//step/name[text()=stepn]').getparent()

'NoneType' object has no attribute 'getparent'

回答1:


stepn = 'Design'
step = xml_obj.find('.//step/name[text()={}]'.format(stepn)).getparent()

when you use ' ', it's a string , any element in string will be treated as a string, not a variable.

Use format to add variable to string



来源:https://stackoverflow.com/questions/41985178/lxml-find-element-by-name-but-use-variable-in-search

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!