页面命令
页面命令处理页面导航、生命周期事件和页面操作。
概述
页面命令模块提供页面间导航、管理页面生命周期、处理 JavaScript 执行以及控制页面行为的功能。
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.
用法
“Tab”类广泛使用页面命令进行导航和页面管理:
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)
关键功能
页面命令模块提供以下函数:
导航
navigate()
- 访问URLreload()
- 重新加载当前页面go_back()
- 后退一步go_forward()
- 前进一步stop_loading()
- 停止页面加载
页面生命周期
enable()
/disable()
- 启用/禁用页面事件get_frame_tree()
- 获取页面框架结构get_navigation_history()
- 获取导航历史记录
内容管理
get_resource_content()
- 获取页面资源内容search_in_resource()
- 在页面资源内搜索set_document_content()
- 设置页面 HTML 内容
截图和 PDF
capture_screenshot()
- 页面截图print_to_pdf()
- 将页面保存为PDFcapture_snapshot()
- 页面快照
JavaScript 执行
add_script_to_evaluate_on_new_document()
- 添加启动脚本(在网页加载前注入js)remove_script_to_evaluate_on_new_document()
- 移除启动脚本
页面设置
set_lifecycle_events_enabled()
- 控制生命周期事件set_ad_blocking_enabled()
- 启用/禁用广告拦截set_bypass_csp()
- 绕过内容安全策略
高级功能
框架管理
# 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}")
资源拦截
# Get resource content
content = await get_resource_content(
connection,
frame_id=frame_id,
url="https://example.com/script.js"
)
页面事件
页面命令可与各种页面事件配合使用:
- Page.loadEventFired
- 页面加载完成
- Page.domContentEventFired
- DOM 内容已加载
- Page.frameNavigated
- 框架访问结束
- Page.frameStartedLoading
- 框架加载开始
!!! 小提示“Tab 类集成”
大多数页面操作都可以通过 Tab
类方法实现,例如 tab.go_to()
、tab.reload()
和 tab.screenshot()
,这些方法提供了更便捷的 API。