Skip to main content
The AttributesHandler class is a read-only mapping used for handling HTML element attributes. It provides a performance-optimized alternative to standard dictionaries while adding useful methods for attribute manipulation.

Overview

AttributesHandler is returned by the Selector.attrib property and provides dictionary-like access to element attributes. All string values are automatically wrapped in TextHandler objects for enhanced text processing capabilities.

Constructor

Any
default:"None"
A dictionary or mapping object containing attribute key-value pairs. String values are automatically converted to TextHandler objects
Any
Additional keyword arguments to add as attributes. String values are automatically converted to TextHandler objects
Example:

Properties

json_string

Convert current attributes to JSON bytes. Returns: JSON-encoded bytes representation of the attributes Raises: Exception if attributes are not JSON serializable Example:

Methods

get()

Get an attribute value by key, with optional default.
str
required
The attribute name to retrieve
Any
default:"None"
The default value to return if the key doesn’t exist
Returns: A TextHandler containing the attribute value, or the default value if not found Example:

search_values()

Search current attributes by values and return a dictionary of each matching item.
str
required
The keyword to search for in the attribute values
bool
default:"false"
If True, searches for keyword contained in values. If False, requires exact match
Returns: A generator yielding AttributesHandler objects for each match, each containing a single key-value pair Example:

Dictionary-like Operations

Accessing Items

Checking Membership

Iteration

Length

Converting to Dictionary

Since AttributesHandler is read-only, if you need to modify attributes, convert it to a standard dictionary:

TextHandler Integration

All attribute values are TextHandler objects, providing access to enhanced string methods:

String Representation

Common Use Cases

Extract CSS Classes

Get Data Attributes

Search for Patterns

Validate Attributes

Notes

  • AttributesHandler is read-only and backed by MappingProxyType for performance
  • All string values are automatically wrapped in TextHandler for enhanced functionality
  • The class implements the Mapping interface, supporting all standard mapping operations
  • Converting to a dictionary with dict() is required if you need to modify attributes
  • Empty AttributesHandler evaluates to False in boolean contexts

See Also

  • TextHandler - For enhanced string operations on attribute values
  • Selector - The main class that uses AttributesHandler via the attrib property