
May 29, 2022
10:14 p.m.
Through trial and error, it looks like xpath() returns an array, even if only one element is found in the tree. This works: HERE = template_tree.xpath('//here') if len(HERE): print("HERE:",HERE) parent = HERE[0].getparent() html_tree = lxml.html.fragment_fromstring("<div>blah</div>", parser=lxml.html.HTMLParser()) parent.insert(parent.index(HERE[0]),html_tree) parent.remove(HERE[0]) print(et.dump(template_root)) else: print("Not HERE")