网页元素
pydoll.elements.web_element.WebElement
Bases: FindElementsMixin
DOM element wrapper for browser automation.
Provides comprehensive functionality for element interaction, inspection, and manipulation using Chrome DevTools Protocol commands.
Initialize WebElement wrapper.
PARAMETER | DESCRIPTION |
---|---|
object_id
|
Unique CDP object identifier for this DOM element.
TYPE:
|
connection_handler
|
Connection instance for browser communication.
TYPE:
|
method
|
Search method used to find this element (for debugging).
TYPE:
|
selector
|
Selector string used to find this element (for debugging).
TYPE:
|
attributes_list
|
Flat list of alternating attribute names and values.
TYPE:
|
bounds
async
property
Element's bounding box coordinates.
Returns coordinates in CSS pixels relative to document origin.
get_bounds_using_js
async
Get element bounds using JavaScript getBoundingClientRect().
Returns coordinates relative to viewport (alternative to bounds property).
get_children_elements
async
Retrieve all direct and nested child elements of this element.
PARAMETER | DESCRIPTION |
---|---|
max_depth
|
Maximum depth to traverse when finding children. Defaults to 1 for direct children only.
TYPE:
|
tag_filter
|
List of HTML tag names to filter results. If empty, returns all child elements regardless of tag. Defaults to [].
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
list[WebElement]
|
list[WebElement]: List of child WebElement objects found within the specified depth and matching the tag filter criteria. |
RAISES | DESCRIPTION |
---|---|
ElementNotFound
|
If no child elements are found for this element and raise_exc is True. |
get_siblings_elements
async
Retrieve all sibling elements of this element (elements at the same DOM level).
PARAMETER | DESCRIPTION |
---|---|
tag_filter
|
List of HTML tag names to filter results. If empty, returns all sibling elements regardless of tag. Defaults to [].
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
list[WebElement]
|
list[WebElement]: List of sibling WebElement objects that share the same parent as this element and match the tag filter criteria. |
RAISES | DESCRIPTION |
---|---|
ElementNotFound
|
If no sibling elements are found for this element |
take_screenshot
async
Capture screenshot of this element only.
Automatically scrolls element into view before capturing.
get_attribute
Get element attribute value.
Note
Only provides attributes available when element was located. For dynamic attributes, consider using JavaScript execution.
wait_until
async
Wait for element to meet specified conditions.
RAISES | DESCRIPTION |
---|---|
ValueError
|
If neither |
WaitElementTimeout
|
If the condition is not met within |
click_using_js
async
Click element using JavaScript click() method.
RAISES | DESCRIPTION |
---|---|
ElementNotVisible
|
If element is not visible. |
ElementNotInteractable
|
If element couldn't be clicked. |
Note
For
click
async
Click element using simulated mouse events.
PARAMETER | DESCRIPTION |
---|---|
x_offset
|
Horizontal offset from element center.
TYPE:
|
y_offset
|
Vertical offset from element center.
TYPE:
|
hold_time
|
Duration to hold mouse button down.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ElementNotVisible
|
If element is not visible. |
Note
For
insert_text
async
Insert text in single operation (faster but less realistic than typing).
Note
Element should already be focused for text to be inserted correctly.
set_input_files
async
Set file paths for file input element.
PARAMETER | DESCRIPTION |
---|---|
files
|
list of absolute file paths to existing files.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ElementNotAFileInput
|
If element is not a file input. |
type_text
async
Type text character by character with realistic timing.
More realistic than insert_text() but slower.
key_down
async
Send key down event.
Note
Only sends key down without release. Pair with key_up() for complete keypress.
press_keyboard_key
async
Press and release keyboard key with configurable timing.
Better for special keys (Enter, Tab, etc.) than type_text().
is_visible
async
Check if element is visible using comprehensive JavaScript visibility test.
is_on_top
async
Check if element is topmost at its center point (not covered by overlays).
is_interactable
async
Check if element is interactable based on visibility and position.
execute_script
async
Execute JavaScript in element context.
Element is available as 'this' within the script.
find
async
find(id: Optional[str] = ..., class_name: Optional[str] = ..., name: Optional[str] = ..., tag_name: Optional[str] = ..., text: Optional[str] = ..., timeout: int = ..., find_all: Literal[False] = False, raise_exc: Literal[True] = True, **attributes: dict[str, str]) -> WebElement
find(id: Optional[str] = ..., class_name: Optional[str] = ..., name: Optional[str] = ..., tag_name: Optional[str] = ..., text: Optional[str] = ..., timeout: int = ..., find_all: Literal[True] = True, raise_exc: Literal[True] = True, **attributes: dict[str, str]) -> list[WebElement]
find(id: Optional[str] = ..., class_name: Optional[str] = ..., name: Optional[str] = ..., tag_name: Optional[str] = ..., text: Optional[str] = ..., timeout: int = ..., find_all: Literal[True] = True, raise_exc: Literal[False] = False, **attributes: dict[str, str]) -> Optional[list[WebElement]]
find(id: Optional[str] = ..., class_name: Optional[str] = ..., name: Optional[str] = ..., tag_name: Optional[str] = ..., text: Optional[str] = ..., timeout: int = ..., find_all: Literal[False] = False, raise_exc: Literal[False] = False, **attributes: dict[str, str]) -> Optional[WebElement]
find(id: Optional[str] = ..., class_name: Optional[str] = ..., name: Optional[str] = ..., tag_name: Optional[str] = ..., text: Optional[str] = ..., timeout: int = ..., find_all: bool = ..., raise_exc: bool = ..., **attributes: dict[str, str]) -> Union[WebElement, list[WebElement], None]
find(id=None, class_name=None, name=None, tag_name=None, text=None, timeout=0, find_all=False, raise_exc=True, **attributes)
Find element(s) using combination of common HTML attributes.
Flexible element location using standard attributes. Multiple attributes can be combined for specific selectors (builds XPath when multiple specified).
PARAMETER | DESCRIPTION |
---|---|
id
|
Element ID attribute value.
TYPE:
|
class_name
|
CSS class name to match.
TYPE:
|
name
|
Element name attribute value.
TYPE:
|
tag_name
|
HTML tag name (e.g., "div", "input").
TYPE:
|
text
|
Text content to match within element.
TYPE:
|
timeout
|
Maximum seconds to wait for elements to appear.
TYPE:
|
find_all
|
If True, returns all matches; if False, first match only.
TYPE:
|
raise_exc
|
Whether to raise exception if no elements found.
TYPE:
|
**attributes
|
Additional HTML attributes to match.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[WebElement, list[WebElement], None]
|
WebElement, list[WebElement], or None based on find_all and raise_exc. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If no search criteria provided. |
ElementNotFound
|
If no elements found and raise_exc=True. |
WaitElementTimeout
|
If timeout specified and no elements appear in time. |
query
async
query(expression: str, timeout: int = ..., find_all: Literal[False] = False, raise_exc: Literal[True] = True) -> WebElement
query(expression: str, timeout: int = ..., find_all: Literal[False] = False, raise_exc: Literal[False] = False) -> Optional[WebElement]
query(expression: str, timeout: int = ..., find_all: Literal[True] = True, raise_exc: Literal[True] = True) -> list[WebElement]
query(expression: str, timeout: int = ..., find_all: Literal[True] = True, raise_exc: Literal[False] = False) -> Optional[list[WebElement]]
query(expression: str, timeout: int = ..., find_all: bool = ..., raise_exc: bool = ...) -> Union[WebElement, list[WebElement], None]
Find element(s) using raw CSS selector or XPath expression.
Direct access using CSS or XPath syntax. Selector type automatically determined based on expression pattern.
PARAMETER | DESCRIPTION |
---|---|
expression
|
Selector expression (CSS, XPath, ID with #, class with .).
TYPE:
|
timeout
|
Maximum seconds to wait for elements to appear.
TYPE:
|
find_all
|
If True, returns all matches; if False, first match only.
TYPE:
|
raise_exc
|
Whether to raise exception if no elements found.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[WebElement, list[WebElement], None]
|
WebElement, list[WebElement], or None based on find_all and raise_exc. |
RAISES | DESCRIPTION |
---|---|
ElementNotFound
|
If no elements found and raise_exc=True. |
WaitElementTimeout
|
If timeout specified and no elements appear in time. |
find_or_wait_element
async
Core element finding method with optional waiting capability.
Searches for elements with flexible waiting. If timeout specified, repeatedly attempts to find elements with 0.5s delays until success or timeout. Used by higher-level find() and query() methods.
PARAMETER | DESCRIPTION |
---|---|
by
|
Selector strategy (CSS_SELECTOR, XPATH, ID, etc.).
TYPE:
|
value
|
Selector value to locate element(s).
TYPE:
|
timeout
|
Maximum seconds to wait (0 = no waiting).
TYPE:
|
find_all
|
If True, returns all matches; if False, first match only.
TYPE:
|
raise_exc
|
Whether to raise exception if no elements found.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[WebElement, list[WebElement], None]
|
WebElement, list[WebElement], or None based on find_all and raise_exc. |
RAISES | DESCRIPTION |
---|---|
ElementNotFound
|
If no elements found with timeout=0 and raise_exc=True. |
WaitElementTimeout
|
If elements not found within timeout and raise_exc=True. |