Page Commands
Page commands handle page navigation, lifecycle events, and page-level operations.
Overview
The page commands module provides functionality for navigating between pages, managing page lifecycle, handling JavaScript execution, and controlling page behavior.
pydoll.commands.page_commands
PageCommands
This class encapsulates the page commands of the Chrome DevTools Protocol (CDP).
CDP's Page domain allows for interacting with browser pages, including navigation, content manipulation, and page state monitoring. These commands provide powerful capabilities for web automation, testing, and debugging.
The commands defined in this class provide functionality for: - Navigating to URLs and managing page history - Capturing screenshots and generating PDFs - Handling JavaScript dialogs - Enabling and controlling page events - Managing download behavior - Manipulating page content and state
add_script_to_evaluate_on_new_document
staticmethod
add_script_to_evaluate_on_new_document(source, world_name=None, include_command_line_api=None, run_immediately=None)
Creates a command to add a script that will be evaluated when a new document is created.
PARAMETER | DESCRIPTION |
---|---|
source
|
Script source to be evaluated when a new document is created.
TYPE:
|
world_name
|
If specified, creates an isolated world with the given name.
TYPE:
|
include_command_line_api
|
Whether to include command line API.
TYPE:
|
run_immediately
|
Whether to run the script immediately on existing contexts.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[AddScriptToEvaluateOnNewDocumentResponse]
|
Command[AddScriptToEvaluateOnNewDocumentResponse]: Command object with the identifier of the added script. |
capture_screenshot
staticmethod
capture_screenshot(format=None, quality=None, clip=None, from_surface=None, capture_beyond_viewport=None, optimize_for_speed=None)
Creates a command to capture a screenshot of the current page.
PARAMETER | DESCRIPTION |
---|---|
format
|
Image compression format (jpeg, png, or webp).
TYPE:
|
quality
|
Compression quality from 0-100 (jpeg only).
TYPE:
|
clip
|
Region of the page to capture.
TYPE:
|
from_surface
|
Capture from the surface, not the view.
TYPE:
|
capture_beyond_viewport
|
Capture beyond the viewport.
TYPE:
|
optimize_for_speed
|
Optimize for speed, not for size.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[CaptureScreenshotResponse]
|
Command[CaptureScreenshotResponse]: Command object with base64-encoded image data. |
close
staticmethod
Creates a command to close the current page.
RETURNS | DESCRIPTION |
---|---|
Command[Response]
|
Command[Response]: Command object to close the page. |
create_isolated_world
staticmethod
Creates a command to create an isolated world for the given frame.
PARAMETER | DESCRIPTION |
---|---|
frame_id
|
ID of the frame in which to create the isolated world.
TYPE:
|
world_name
|
Name to be reported in the Execution Context.
TYPE:
|
grant_universal_access
|
Whether to grant universal access.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[CreateIsolatedWorldResponse]
|
Command[CreateIsolatedWorldResponse]: Command object with the execution context ID. |
disable
staticmethod
Creates a command to disable page domain notifications.
RETURNS | DESCRIPTION |
---|---|
Command[Response]
|
Command[Response]: Command object to disable the Page domain. |
enable
staticmethod
Creates a command to enable page domain notifications.
PARAMETER | DESCRIPTION |
---|---|
enable_file_chooser_opened_event
|
Whether to emit Page.fileChooserOpened event.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[Response]
|
Command[Response]: Command object to enable the Page domain. |
get_app_manifest
staticmethod
Creates a command to get the manifest for the current document.
RETURNS | DESCRIPTION |
---|---|
Command[GetAppManifestResponse]
|
Command[GetAppManifestResponse]: Command object with manifest information. |
get_frame_tree
staticmethod
Creates a command to get the frame tree for the current page.
RETURNS | DESCRIPTION |
---|---|
Command[GetFrameTreeResponse]
|
Command[GetFrameTreeResponse]: Command object with frame tree information. |
get_layout_metrics
staticmethod
Creates a command to get layout metrics for the page.
RETURNS | DESCRIPTION |
---|---|
Command[GetLayoutMetricsResponse]
|
Command[GetLayoutMetricsResponse]: Command object with layout metrics. |
get_navigation_history
staticmethod
Creates a command to get the navigation history for the current page.
RETURNS | DESCRIPTION |
---|---|
Command[GetNavigationHistoryResponse]
|
Command[GetNavigationHistoryResponse]: Command object with navigation history. |
handle_javascript_dialog
staticmethod
Creates a command to handle a JavaScript dialog.
PARAMETER | DESCRIPTION |
---|---|
accept
|
Whether to accept or dismiss the dialog.
TYPE:
|
prompt_text
|
Text to enter in prompt dialogs.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[Response]
|
Command[Response]: Command object to handle a JavaScript dialog. |
navigate
staticmethod
Creates a command to navigate to a specific URL.
PARAMETER | DESCRIPTION |
---|---|
url
|
URL to navigate to.
TYPE:
|
referrer
|
Referrer URL.
TYPE:
|
transition_type
|
Intended transition type.
TYPE:
|
frame_id
|
Frame ID to navigate.
TYPE:
|
referrer_policy
|
Referrer policy.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[NavigateResponse]
|
Command[NavigateResponse]: Command object to navigate to a URL. |
navigate_to_history_entry
staticmethod
Creates a command to navigate to a specific history entry.
PARAMETER | DESCRIPTION |
---|---|
entry_id
|
ID of the history entry to navigate to.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[Response]
|
Command[Response]: Command object to navigate to a history entry. |
print_to_pdf
staticmethod
print_to_pdf(landscape=None, display_header_footer=None, print_background=None, scale=None, paper_width=None, paper_height=None, margin_top=None, margin_bottom=None, margin_left=None, margin_right=None, page_ranges=None, header_template=None, footer_template=None, prefer_css_page_size=None, transfer_mode=None, generate_tagged_pdf=None, generate_document_outline=None)
Creates a command to print the current page to PDF.
PARAMETER | DESCRIPTION |
---|---|
landscape
|
Paper orientation.
TYPE:
|
display_header_footer
|
Display header and footer.
TYPE:
|
print_background
|
Print background graphics.
TYPE:
|
scale
|
Scale of the webpage rendering.
TYPE:
|
paper_width
|
Paper width in inches.
TYPE:
|
paper_height
|
Paper height in inches.
TYPE:
|
margin_top
|
Top margin in inches.
TYPE:
|
margin_bottom
|
Bottom margin in inches.
TYPE:
|
margin_left
|
Left margin in inches.
TYPE:
|
margin_right
|
Right margin in inches.
TYPE:
|
page_ranges
|
Paper ranges to print, e.g., '1-5, 8, 11-13'.
TYPE:
|
header_template
|
HTML template for the print header.
TYPE:
|
footer_template
|
HTML template for the print footer.
TYPE:
|
prefer_css_page_size
|
Whether to prefer page size as defined by CSS.
TYPE:
|
transfer_mode
|
Transfer mode.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[PrintToPDFResponse]
|
Command[PrintToPDFResponse]: Command object to print the page to PDF. |
reload
staticmethod
Creates a command to reload the current page.
PARAMETER | DESCRIPTION |
---|---|
ignore_cache
|
If true, browser cache is ignored.
TYPE:
|
script_to_evaluate_on_load
|
Script to be injected into the page on load.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[Response]
|
Command[Response]: Command object to reload the page. |
reset_navigation_history
staticmethod
Creates a command to reset the navigation history.
remove_script_to_evaluate_on_new_document
staticmethod
Creates a command to remove a script that was added to be evaluated on new documents.
PARAMETER | DESCRIPTION |
---|---|
identifier
|
Identifier of the script to remove.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[Response]
|
Command[Response]: Command object to remove a script. |
set_bypass_csp
staticmethod
Creates a command to toggle bypassing page CSP.
PARAMETER | DESCRIPTION |
---|---|
enabled
|
Whether to bypass page CSP.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[Response]
|
Command[Response]: Command object to toggle bypassing page CSP. |
set_document_content
staticmethod
Creates a command to set the document content of a frame.
PARAMETER | DESCRIPTION |
---|---|
frame_id
|
Frame ID to set the document content for.
TYPE:
|
html
|
HTML content to set.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[Response]
|
Command[Response]: Command object to set the document content. |
set_intercept_file_chooser_dialog
staticmethod
Creates a command to set whether to intercept file chooser dialogs.
PARAMETER | DESCRIPTION |
---|---|
enabled
|
Whether to intercept file chooser dialogs.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[Response]
|
Command[Response]: Command object to set file chooser dialog interception. |
set_lifecycle_events_enabled
staticmethod
Creates a command to enable/disable lifecycle events.
PARAMETER | DESCRIPTION |
---|---|
enabled
|
Whether to enable lifecycle events.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[Response]
|
Command[Response]: Command object to enable/disable lifecycle events. |
stop_loading
staticmethod
Creates a command to stop loading the page.
RETURNS | DESCRIPTION |
---|---|
Command[Response]
|
Command[Response]: Command object to stop loading the page. |
add_compilation_cache
staticmethod
Creates a command to add a compilation cache entry.
Experimental: This method is experimental and may be subject to change.
PARAMETER | DESCRIPTION |
---|---|
url
|
URL for which to add the compilation cache entry.
TYPE:
|
data
|
Base64-encoded data.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[Response]
|
Command[Response]: Command object to add a compilation cache entry. |
capture_snapshot
staticmethod
Creates a command to capture a snapshot of the page.
Experimental: This method is experimental and may be subject to change.
PARAMETER | DESCRIPTION |
---|---|
format
|
Format of the snapshot (only 'mhtml' is supported).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[CaptureSnapshotResponse]
|
Command[CaptureSnapshotResponse]: Command object to capture a snapshot. |
clear_compilation_cache
staticmethod
Creates a command to clear the compilation cache.
generate_test_report
staticmethod
Creates a command to generate a test report.
Experimental: This method is experimental and may be subject to change.
PARAMETER | DESCRIPTION |
---|---|
message
|
Message to be displayed in the report.
TYPE:
|
group
|
Group label for the report.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[Response]
|
Command[Response]: Command object to generate a test report. |
get_ad_script_ancestry_ids
staticmethod
Creates a command to get the ad script ancestry IDs for a given frame.
Experimental: This method is experimental and may be subject to change.
PARAMETER | DESCRIPTION |
---|---|
frame_id
|
ID of the frame to get ad script ancestry IDs for.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[GetAdScriptAncestryIdsResponse]
|
Command[GetAdScriptAncestryIdsResponse]: Command object to get ad script ancestry IDs. |
get_app_id
staticmethod
Creates a command to get the app ID.
Experimental: This method is experimental and may be subject to change.
PARAMETER | DESCRIPTION |
---|---|
app_id
|
App ID for verification.
TYPE:
|
recommended_id
|
Recommended app ID.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[GetAppIdResponse]
|
Command[GetAppIdResponse]: Command object to get the app ID. |
get_installability_errors
staticmethod
Creates a command to get the installability errors.
get_origin_trials
staticmethod
Creates a command to get origin trials for a given origin.
Experimental: This method is experimental and may be subject to change.
PARAMETER | DESCRIPTION |
---|---|
frame_id
|
Frame ID to get trials for.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Command[GetOriginTrialsResponse]
|
Command[GetOriginTrialsResponse]: Command object to get origin trials. |
get_permissions_policy_state
staticmethod
Creates a command to get the permissions policy state.
get_resource_content
staticmethod
Creates a command to get the resource content.
produce_compilation_cache
staticmethod
Creates a command to produce a compilation cache entry.
screencast_frame_ack
staticmethod
Creates a command to acknowledge a screencast frame.
search_in_resource
staticmethod
Creates a command to search for a string in a resource.
set_ad_blocking_enabled
staticmethod
Creates a command to set ad blocking enabled.
set_font_families
staticmethod
Creates a command to set font families.
set_prerendering_allowed
staticmethod
Creates a command to set prerendering allowed.
set_rph_registration_mode
staticmethod
Creates a command to set the RPH registration mode.
set_spc_transaction_mode
staticmethod
Creates a command to set the SPC transaction mode.
set_web_lifecycle_state
staticmethod
Creates a command to set the web lifecycle state.
start_screencast
staticmethod
Creates a command to start a screencast.
Usage
Page commands are used extensively by the Tab
class for navigation and page management:
from pydoll.commands.page_commands import navigate, reload, enable
from pydoll.connection.connection_handler import ConnectionHandler
# Navigate to a URL
connection = ConnectionHandler()
await enable(connection) # Enable page events
await navigate(connection, url="https://example.com")
# Reload the page
await reload(connection)
Key Functionality
The page commands module provides functions for:
Navigation
navigate()
- Navigate to URLsreload()
- Reload current pagego_back()
- Navigate back in historygo_forward()
- Navigate forward in historystop_loading()
- Stop page loading
Page Lifecycle
enable()
/disable()
- Enable/disable page eventsget_frame_tree()
- Get page frame structureget_navigation_history()
- Get navigation history
Content Management
get_resource_content()
- Get page resource contentsearch_in_resource()
- Search within page resourcesset_document_content()
- Set page HTML content
Screenshots & PDF
capture_screenshot()
- Take page screenshotsprint_to_pdf()
- Generate PDF from pagecapture_snapshot()
- Capture page snapshots
JavaScript Execution
add_script_to_evaluate_on_new_document()
- Add startup scriptsremove_script_to_evaluate_on_new_document()
- Remove startup scripts
Page Settings
set_lifecycle_events_enabled()
- Control lifecycle eventsset_ad_blocking_enabled()
- Enable/disable ad blockingset_bypass_csp()
- Bypass Content Security Policy
Advanced Features
Frame Management
# Get all frames in the page
frame_tree = await get_frame_tree(connection)
for frame in frame_tree.child_frames:
print(f"Frame: {frame.frame.url}")
Resource Interception
# Get resource content
content = await get_resource_content(
connection,
frame_id=frame_id,
url="https://example.com/script.js"
)
Page Events
The page commands work with various page events:
- Page.loadEventFired
- Page load completed
- Page.domContentEventFired
- DOM content loaded
- Page.frameNavigated
- Frame navigation
- Page.frameStartedLoading
- Frame loading started
Tab Class Integration
Most page operations are available through the Tab
class methods like tab.go_to()
, tab.reload()
, and tab.screenshot()
which provide a more convenient API.