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 objectsAny
Additional keyword arguments to add as attributes. String values are automatically converted to
TextHandler objectsProperties
json_string
Methods
get()
str
required
The attribute name to retrieve
Any
default:"None"
The default value to return if the key doesn’t exist
TextHandler containing the attribute value, or the default value if not found
Example:
search_values()
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
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
SinceAttributesHandler is read-only, if you need to modify attributes, convert it to a standard dictionary:
TextHandler Integration
All attribute values areTextHandler objects, providing access to enhanced string methods:
String Representation
Common Use Cases
Extract CSS Classes
Get Data Attributes
Search for Patterns
Validate Attributes
Notes
AttributesHandleris read-only and backed byMappingProxyTypefor performance- All string values are automatically wrapped in
TextHandlerfor enhanced functionality - The class implements the
Mappinginterface, supporting all standard mapping operations - Converting to a dictionary with
dict()is required if you need to modify attributes - Empty
AttributesHandlerevaluates toFalsein boolean contexts
See Also
- TextHandler - For enhanced string operations on attribute values
- Selector - The main class that uses
AttributesHandlervia theattribproperty