Element

Elements are returned from querying methods. They have the following properties to retrieve their data.

name

Gets the name of the Element.

html = HTML("<span>Dormouse</span>")
span_element = html.root_element

assert span_element.name == "span"

id

Gets the id of the Element.

html = HTML("<span id='dormouse'>Dormouse</span>")
span_element = html.root_element

assert span_element.id == "dormouse"

attributes

Get attributes

html = HTML("<button class="mt-2 pb-2" disabled>Wake up</button>")
button_element = html.root_element

assert button_element.attributes == {"class": "mt-2 pb-2", "disabled": True}

Set attributes

html = HTML("<button>Go back to sleep</button>")
button_element = html.root_element
button_element.attributes = {"class": "mt-2 pb-2", "disabled": True}

assert str(button_element) == '<button class="mt-2 pb-2" disabled>Go back to sleep</button>'

classes

Gets a list of classes for the element.

html = HTML("<button class="mt-2 pb-2">Wake Up</button>")
button_element = html.root_element

assert button_element.classes == ["mt-2", "pb-2"]

text

Get text context

html = HTML("<button>Wake Up</button>")
button_element = html.root_element

assert button_element.text == "Wake Up"

Set text content

html = HTML("<button>Wake Up</button>")
button_element = html.root_element

button_element.text = "Go back to sleep"

assert str(button_element) == "<button>Go back to sleep</button>"