Tab
pydoll.browser.tab.Tab
Bases: FindElementsMixin
Controls a browser tab via Chrome DevTools Protocol.
Primary interface for web page automation including navigation, DOM manipulation, JavaScript execution, event handling, network monitoring, and specialized tasks like Cloudflare bypass.
This class implements a singleton pattern based on target_id to ensure only one Tab instance exists per browser tab.
Initialize tab controller for existing browser tab.
PARAMETER | DESCRIPTION |
---|---|
browser
|
Browser instance that created this tab.
TYPE:
|
connection_port
|
CDP WebSocket port.
TYPE:
|
target_id
|
CDP target identifier for this tab.
TYPE:
|
browser_context_id
|
Optional browser context ID.
TYPE:
|
network_events_enabled
property
Whether CDP Network domain events are enabled.
fetch_events_enabled
property
Whether CDP Fetch domain events (request interception) are enabled.
runtime_events_enabled
property
Whether CDP Runtime domain events are enabled.
intercept_file_chooser_dialog_enabled
property
Whether file chooser dialog interception is active.
current_url
async
property
Get current page URL (reflects redirects and client-side navigation).
get_instance
classmethod
Get existing Tab instance for target_id if it exists.
PARAMETER | DESCRIPTION |
---|---|
target_id
|
Target ID to look up.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Optional[Tab]
|
Existing Tab instance or None if not found. |
get_all_instances
classmethod
Get all active Tab instances.
RETURNS | DESCRIPTION |
---|---|
dict[str, Tab]
|
Dictionary mapping target_id to Tab instances. |
enable_page_events
async
Enable CDP Page domain events (load, navigation, dialogs, etc.).
enable_network_events
async
Enable CDP Network domain events (requests, responses, etc.).
enable_fetch_events
async
Enable CDP Fetch domain for request interception.
PARAMETER | DESCRIPTION |
---|---|
handle_auth
|
Intercept authentication challenges.
TYPE:
|
resource_type
|
Filter by resource type (all if None).
TYPE:
|
request_stage
|
When to intercept (Request/Response).
TYPE:
|
Note
Intercepted requests must be explicitly continued or timeout.
enable_dom_events
async
Enable CDP DOM domain events (document structure changes).
enable_intercept_file_chooser_dialog
async
Enable file chooser dialog interception for automated uploads.
Note
Use expect_file_chooser context manager for convenience.
enable_auto_solve_cloudflare_captcha
async
enable_auto_solve_cloudflare_captcha(custom_selector=None, time_before_click=2, time_to_wait_captcha=5)
Enable automatic Cloudflare Turnstile captcha bypass.
PARAMETER | DESCRIPTION |
---|---|
custom_selector
|
Custom captcha selector (default: cf-turnstile class).
TYPE:
|
time_before_click
|
Delay before clicking captcha (default 2s).
TYPE:
|
time_to_wait_captcha
|
Timeout for captcha detection (default 5s).
TYPE:
|
disable_fetch_events
async
Disable CDP Fetch domain and release paused requests.
disable_intercept_file_chooser_dialog
async
Disable file chooser dialog interception.
disable_auto_solve_cloudflare_captcha
async
Disable automatic Cloudflare Turnstile captcha bypass.
close
async
Close this browser tab.
Note
Tab instance becomes invalid after calling this method.
get_frame
async
Get Tab object for interacting with iframe content.
PARAMETER | DESCRIPTION |
---|---|
frame
|
Tab representing the iframe tag.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
IFrame
|
Tab instance configured for iframe interaction. |
RAISES | DESCRIPTION |
---|---|
NotAnIFrame
|
If element is not an iframe. |
InvalidIFrame
|
If iframe lacks valid src attribute. |
IFrameNotFound
|
If iframe target not found in browser. |
get_network_response_body
async
Get the response body for a given request ID.
PARAMETER | DESCRIPTION |
---|---|
request_id
|
Request ID to get the response body for.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
str
|
The response body for the given request ID. |
RAISES | DESCRIPTION |
---|---|
NetworkEventsNotEnabled
|
If network events are not enabled. |
get_network_logs
async
Get network logs.
PARAMETER | DESCRIPTION |
---|---|
filter
|
Filter to apply to the network logs.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
list[NetworkLog]
|
The network logs. |
RAISES | DESCRIPTION |
---|---|
NetworkEventsNotEnabled
|
If network events are not enabled. |
set_cookies
async
Set multiple cookies for current page.
PARAMETER | DESCRIPTION |
---|---|
cookies
|
Cookie parameters (name/value required, others optional).
TYPE:
|
Note
Defaults to current page's domain if not specified.
go_to
async
Navigate to URL and wait for loading to complete.
Refreshes if URL matches current page.
PARAMETER | DESCRIPTION |
---|---|
url
|
Target URL to navigate to.
TYPE:
|
timeout
|
Maximum seconds to wait for page load (default 300).
TYPE:
|
RAISES | DESCRIPTION |
---|---|
PageLoadTimeout
|
If page doesn't finish loading within timeout. |
refresh
async
Reload current page and wait for completion.
PARAMETER | DESCRIPTION |
---|---|
ignore_cache
|
Bypass browser cache if True.
TYPE:
|
script_to_evaluate_on_load
|
JavaScript to execute after load.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
PageLoadTimeout
|
If page doesn't finish loading within timeout. |
take_screenshot
async
Capture screenshot of current page.
PARAMETER | DESCRIPTION |
---|---|
path
|
File path for screenshot (extension determines format).
TYPE:
|
quality
|
Image quality 0-100 (default 100).
TYPE:
|
as_base64
|
Return as base64 string instead of saving file.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Optional[str]
|
Base64 screenshot data if as_base64=True, None otherwise. |
RAISES | DESCRIPTION |
---|---|
InvalidFileExtension
|
If file extension not supported. |
ValueError
|
If path is None and as_base64 is False. |
print_to_pdf
async
print_to_pdf(path, landscape=False, display_header_footer=False, print_background=True, scale=1.0, as_base64=False)
Generate PDF of current page.
PARAMETER | DESCRIPTION |
---|---|
path
|
File path for PDF output.
TYPE:
|
landscape
|
Use landscape orientation.
TYPE:
|
display_header_footer
|
Include header/footer.
TYPE:
|
print_background
|
Include background graphics.
TYPE:
|
scale
|
Scale factor (0.1-2.0).
TYPE:
|
as_base64
|
Return as base64 string instead of saving.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Optional[str]
|
Base64 PDF data if as_base64=True, None otherwise. |
has_dialog
async
Check if JavaScript dialog is currently displayed.
Note
Page events must be enabled to detect dialogs.
get_dialog_message
async
Get message text from current JavaScript dialog.
RAISES | DESCRIPTION |
---|---|
NoDialogPresent
|
If no dialog is currently displayed. |
handle_dialog
async
Respond to JavaScript dialog.
PARAMETER | DESCRIPTION |
---|---|
accept
|
Accept/confirm dialog if True, dismiss/cancel if False.
TYPE:
|
prompt_text
|
Text for prompt dialogs (ignored for alert/confirm).
TYPE:
|
RAISES | DESCRIPTION |
---|---|
NoDialogPresent
|
If no dialog is currently displayed. |
Note
Page events must be enabled to handle dialogs.
execute_script
async
execute_script(script: str, element: WebElement) -> CallFunctionOnResponse
Execute JavaScript in page context.
PARAMETER | DESCRIPTION |
---|---|
script
|
JavaScript code to execute.
TYPE:
|
element
|
Element context (use 'argument' in script to reference).
TYPE:
|
Examples:
await page.execute_script('argument.click()', element) await page.execute_script('argument.value = "Hello"', element)
RAISES | DESCRIPTION |
---|---|
InvalidScriptWithElement
|
If script contains 'argument' but no element is provided. |
continue_request
async
continue_request(request_id, url=None, method=None, post_data=None, headers=None, intercept_response=None)
Continue paused request without modifications.
fulfill_request
async
Fulfill request with response data.
expect_file_chooser
async
Context manager for automatic file upload handling.
PARAMETER | DESCRIPTION |
---|---|
files
|
File path(s) for upload.
TYPE:
|
expect_and_bypass_cloudflare_captcha
async
expect_and_bypass_cloudflare_captcha(custom_selector=None, time_before_click=2, time_to_wait_captcha=5)
Context manager for automatic Cloudflare captcha bypass.
PARAMETER | DESCRIPTION |
---|---|
custom_selector
|
Custom captcha selector (default: cf-turnstile class).
TYPE:
|
time_before_click
|
Delay before clicking (default 2s).
TYPE:
|
time_to_wait_captcha
|
Timeout for captcha detection (default 5s).
TYPE:
|
on
async
Register CDP event listener.
Callback runs in background task to prevent blocking.
PARAMETER | DESCRIPTION |
---|---|
event_name
|
CDP event name (e.g., 'Page.loadEventFired').
TYPE:
|
callback
|
Function called on event (sync or async).
TYPE:
|
temporary
|
Remove after first invocation.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
int
|
Callback ID for removal. |
Note
Corresponding domain must be enabled before events fire.
find
async
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
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. |