Skip to main content

Overview

Scrapling provides intuitive methods to traverse the DOM tree. You can move up to parent elements, down to children, sideways to siblings, or iterate through ancestors.

Parent Elements

parent

Access the direct parent of an element.
Returns the direct parent element or None if the element has no parent.

iterancestors()

Iterate through all ancestor elements from parent to root.
Returns a generator that loops over all ancestors of the element, starting with the element’s parent.

find_ancestor()

Find the first ancestor that matches a condition.
Callable[[Selector], bool]
required
A function that takes each ancestor as an argument and returns True/False
Returns the first ancestor that matches the function or None otherwise.

path

Get the full path from root to the current element.
Returns a list of type Selectors that contains the path leading to the current element from the root.

Children Elements

children

Get all direct children of an element.
Returns the direct children elements of the current element or an empty list otherwise.

below_elements

Get all descendants (not just direct children) under the current element.
Returns all elements under the current element in the DOM tree.

Sibling Elements

siblings

Get all sibling elements (other children of the same parent).
Returns other children of the current element’s parent or an empty list otherwise.

next

Get the next sibling element.
Returns the next element after the current element in the parent’s children, or None otherwise.

previous

Get the previous sibling element.
Returns the previous element before the current element in the parent’s children, or None otherwise.

Practical Examples

Extract breadcrumb information using parent navigation:

Table Row Navigation

Navigate through table rows and cells:
Navigate through nested menu structures:

List Navigation

Process list items with context:
Use navigation to find related elements:

Check Element Position

Find Common Ancestor