Table of Contents


Create a new element howto

child2 = etree.SubElement(root, "child2")
child3 = etree.SubElement(root, "child3")

etree.Element() is not the class itself. It's Element factory. To create an element but not the etree._Element, such as HtmlElement, create it manually:

elem = HtmlElement()
elem.tag = 'div'
elem.text = 'Hello, world!'

Clone an element howto

from copy import deepcopy

div = etree.Element("div")

Test if an element has children howto

if len(elem):
    print("The element has children")

Add a new element before/after an element howto


Remove all children howto

def remove_children(elem):
    for c in elem.iterchildren():

Remove an element howto


Test Element Equality howto

def elem_equal(e1, e2):
    return (e1.tag == e2.tag and
            e1.text == e2.text and
            e1.tail == e2.tail and
            e1.attrib == e2.attrib and
            len(e1) == len(e2))
    return all(elem_equal(c1, c2) for c1, c2 in zip(e1, e2))

Surround an element with another tag howto

You should:

  1. Clone the element
  2. Create a new element and add the cloned one as a child
  3. Add the new element previous or next to the original element
  4. Remove the original one
from copy import deepcopy
from lxml.html.builder import DIV

def surround(elem):
    clone = deeopcopy(elem)
    div = DIV(clone)
    return div

Add a child element at very first howto

As element.text is always placed before the first subelement, we should move element.text to subelement.tail:

child = Element('div')
if parent.text:
    child.tail = parent.text
    parent.text = None
parent.insert(0, child)