Element

Overview

Element is a class in Aspose.Html FOSS for Python. Inherits from: Node.

An HTML or XML element node.

This class provides 72 methods for working with Element objects in Python programs. Available methods include: __init__, add_event_listener, after, append, append_child, attach_shadow, before, check_visibility, clone_node, closest, compare_document_position, contains, and 60 additional methods. All public members are accessible to any Python application after installing the Aspose.Html FOSS for Python package. Properties: assigned_slot, attributes, child_element_count, child_nodes, children, class_list, and 43 more.

Properties

NameTypeAccessDescription
node_namestrReadThe qualified tag name (uppercase for HTML elements).
tag_namestrReadThe qualified tag name in uppercase (HTML elements).
local_namestrReadThe local part of the qualified name (lowercase).
namespace_uristr | NoneReadThe namespace URI, or None for no namespace.
style_sheets"StyleSheetList"ReadLive stylesheet list applicable to this element’s document.
prefixstr | NoneReadThe namespace prefix, or None.
idstrReadThe value of the id attribute, or '' if not present.
class_namestrReadThe value of the class attribute, or ''.
class_list"DOMTokenList"ReadLive mutable set of CSS class tokens backed by the class attribute.
dataset"DOMStringMap"ReadLive dict-like view of the element’s data-* custom attributes.
attributesNamedNodeMapReadThe ordered map of Attr objects for this element.
childrenHTMLCollectionReadA live HTMLCollection of Element-type children only.
first_element_childElement | NoneReadThe first Element child, or None.
last_element_childElement | NoneReadThe last Element child, or None.
next_element_siblingElement | NoneReadThe next sibling that is an Element, or None.
previous_element_siblingElement | NoneReadThe previous sibling that is an Element, or None.
child_element_countintReadThe number of Element children.
inner_htmlstrRead/WriteThe serialised HTML content of all children (innerHTML).
outer_htmlstrRead/WriteThe serialised HTML of this element and all its descendants (outerHTML).
text_contentstr | NoneRead/WriteThe concatenation of all descendant Text node data (DFS order).
style"CSSStyleDeclaration"ReadThe inline style of this element as a live CSSStyleDeclaration.
scroll_widthintReadContent scroll width in CSS pixels (stub: 0).
scroll_heightintReadContent scroll height in CSS pixels (stub: 0).
client_widthintReadInner width including padding, excluding border (stub: 0).
client_heightintReadInner height including padding, excluding border (stub: 0).
scroll_topintRead/WriteVertical scroll offset in CSS pixels (stub: 0).
scroll_leftintRead/WriteHorizontal scroll offset in CSS pixels (stub: 0).
offset_widthintReadLayout border-box width in CSS pixels (stub: 0).
offset_heightintReadLayout border-box height in CSS pixels (stub: 0).
offset_topintReadTop offset from offsetParent in CSS pixels (stub: 0).
offset_leftintReadLeft offset from offsetParent in CSS pixels (stub: 0).
offset_parentNoneReadThe offset parent element; always None in headless mode.
client_topintReadWidth of the top CSS border in pixels; always 0 in headless mode.
client_leftintReadWidth of the left CSS border in pixels; always 0 in headless mode.
part"DOMTokenList"ReadLive DOMTokenList reflecting the element part attribute.
shadow_root``ReadAlways None in headless mode.
slotstrRead/WriteReflects the slot content attribute (DOM §4.2.3).
assigned_slot``ReadAlways None in headless mode.
node_typeintReadInteger node type constant from NodeType.
node_valuestr | NoneRead/WriteCharacter data for CharacterData nodes; None otherwise.
parent_nodeNode | NoneReadThe parent node, or None if this node has no parent.
parent_elementElement | NoneReadThe parent node if it is an Element; None otherwise.
is_connectedboolReadWhether this node is part of a document’s node tree (WHATWG DOM §4.4).
owner_documentDocument | NoneReadThe Document this node belongs to, or None for Document itself.
child_nodesNodeListReadA live NodeList of all child nodes.
first_childNode | NoneReadThe first child node, or None.
last_childNode | NoneReadThe last child node, or None.
previous_siblingNode | NoneReadThe preceding sibling node, or None.
next_siblingNode | NoneReadThe following sibling node, or None.

Methods

SignatureDescription
__init__(local_name: str, namespace_uri: str | None, prefix: str | None, owner_document: Document | None)Calls init(local_name, namespace_uri, prefix, owner_document) on this Element instance.
get_computed_style()"ComputedStyleDeclaration"Return phase-2 cascade-resolved declarations for this element.
insert_adjacent_html(position: str, html: str)Insert parsed HTML markup relative to this element.
insert_adjacent_text(position: str, text: str)Insert a text node relative to this element.
insert_adjacent_element(position: str, element: "Element | None")"Element | None"Insert element at position relative to this element.
get_attribute(name: str)str | NoneReturn the value of attribute name, or None if absent.
set_attribute(name: str, value: str)Set attribute name to value, creating it if not present.
remove_attribute(name: str)Remove attribute name.
has_attribute(name: str)boolReturn True if attribute name exists on this element.
get_attribute_node(name: str)"Attr | None"Return the Attr object for attribute name, or None if absent.
set_attribute_node(attr: "Attr")"Attr | None"Insert or replace attr in this element’s attribute set.
remove_attribute_node(attr: "Attr")"Attr"Remove attr from this element’s attribute set and return it.
get_attribute_node_ns(namespace_uri: str | None, local_name: str)"Attr | None"Return the Attr node matching namespace_uri and local_name, or None.
set_attribute_node_ns(attr: "Attr")"Attr | None"Insert or replace a namespaced Attr node.
remove_attribute_node_ns(namespace_uri: str | None, local_name: str)"Attr"Remove and return the Attr node matching namespace_uri and local_name.
get_attribute_ns(namespace_uri: str | None, local_name: str)str | NoneReturn the value of the namespaced attribute, or None if absent.
set_attribute_ns(namespace_uri: str | None, qualified_name: str, value: str)Set a namespaced attribute to value, creating it if not present.
has_attribute_ns(namespace_uri: str | None, local_name: str)boolReturn True if a namespaced attribute with the given local name exists.
remove_attribute_ns(namespace_uri: str | None, local_name: str)Remove the namespaced attribute.
toggle_attribute(name: str, force: bool | None)boolToggle attribute name and return its new presence state.
get_attribute_names()list[str]Return the qualified names of all attributes in collection order.
has_attributes()boolReturn True when this element has one or more attributes.
scroll_into_view(arg: bool | dict | None)Scroll this element into view (no-op in server-side context).
scroll(x_or_options: float | dict | None, y: float | None)Scroll this element to coordinates (no-op in server-side context).
scroll_into_view_if_needed(center_if_needed: bool)Scroll this element into view if it is not already visible (no-op in headless mode).
set_pointer_capture(pointer_id: int)Designate this element as the capture target for the given pointer (no-op).
release_pointer_capture(pointer_id: int)Release pointer capture for the given pointer id (no-op).
has_pointer_capture(pointer_id: int)boolReturn whether this element has pointer capture for the given id.
request_pointer_lock()Request that the pointer be locked to this element (no-op).
request_fullscreen(options: "dict | None")Request that this element be displayed in fullscreen mode (no-op).
check_visibility(options: "dict | None")boolReturn whether this element is visible in the current rendering (always True).
query_selector(selector: str)Element | NoneReturn the first element in the subtree matching selector.
query_selector_all(selector: str)NodeListReturn a static NodeList of all elements matching selector.
matches(selector: str)boolReturn True if this element matches the CSS selector.
webkit_matches_selector(selector: str)boolBrowser-compat alias for :meth:matches.
closest(selector: str)"Element | None"Return the nearest ancestor (inclusive) matching the CSS selector.
get_elements_by_tag_name(qualified_name: str)HTMLCollectionReturn all descendant elements whose tag name matches qualified_name.
get_elements_by_class_name(class_names: str)HTMLCollectionReturn all descendant elements that carry all of the given class tokens.
filter_fn(el: "Element")boolCalls filter_fn(el) on this Element instance.
remove()Remove this element from its parent node.
before()Insert nodes immediately before this element in its parent.
after()Insert nodes immediately after this element in its parent.
replace_with()Replace this element with nodes in its parent.
prepend()Insert nodes before the first child of this element.
append()Append nodes as the last children of this element.
replace_children()Remove all children, then append nodes as new children.
get_bounding_client_rect()"DOMRect"Return the bounding client rectangle (all-zero stub).
get_client_rects()"DOMRectList"Return the client rectangle list (empty in server-side context).
get_animations()list[object]Return active animations for this element (headless stub: always []).
attach_shadow(init: "dict | None")Raise NotSupportedError — shadow trees are not supported in headless mode.
get_root_node(composed: bool)"Node"Return the root of the node’s tree.
has_child_nodes()boolReturn whether this node has at least one direct child node.
lookup_namespace_uri(prefix: str | None)str | NoneReturn the effective namespace URI for prefix in this node context.
lookup_prefix(namespace_uri: str | None)str | NoneReturn the effective prefix for namespace_uri in this node context.
is_default_namespace(namespace_uri: str | None)boolReturn whether namespace_uri equals this node context default namespace.
is_supported(feature: str, version: str | None)boolReturn legacy feature-probe compatibility status.
get_feature(feature: str, version: str | None)object | NoneReturn the legacy feature object for a probe, or None.
set_user_data(key: str, data: object | None, handler: object | None)object | NoneStore or clear node-local user data and return the previous value.
get_user_data(key: str)object | NoneReturn node-local user data for key or None when absent.
append_child(node: Node)NodeAppend node as the last child of this node.
insert_before(node: Node, reference: Node | None)NodeInsert node before reference in the child list.
remove_child(node: Node)NodeRemove node from the child list and return it.
replace_child(new_child: Node, old_child: Node)NodeReplace old_child with new_child.
clone_node(deep: bool)NodeReturn a copy of this node.
normalize()Put this node and its entire subtree into normalized form.
contains(other: Node | None)boolReturn True if other is an inclusive descendant of this node.
compare_document_position(other: "Node")intReturn a bitmask describing the position of other relative to this node.
is_equal_node(other: Node | None)boolReturn True if other has the same structure as this node.
is_same_node(other_node: Node | None)boolReturn True when other_node is this node instance.
add_event_listener(type: str, listener: Callable, capture: bool, once: bool)Register listener for events of type.
remove_event_listener(type: str, listener: Callable, capture: bool)Deregister a previously-registered listener.
dispatch_event(event: "Event")boolDispatch event through the ancestor tree.

See Also