Skip to content

浏览器命令

浏览器命令提供对浏览器实例及其配置的底层控制。

概述

浏览器命令模块处理浏览器级别的操作,例如版本信息、目标管理和浏览器范围的设置。

pydoll.commands.browser_commands

BrowserCommands

BrowserCommands class provides a set of commands to interact with the browser's main functionality based on CDP. These commands allow for managing browser windows, such as closing windows, retrieving window IDs, and adjusting window bounds (size and state).

The commands defined in this class provide functionality for: - Managing browser windows and targets. - Setting permissions and download behavior. - Controlling browser windows (size, state). - Retrieving browser information and versioning.

get_version staticmethod

get_version()

Generates a command to get browser version information.

RETURNS DESCRIPTION
GetVersionCommand

The CDP command that returns browser version details including protocol version, product name, revision, and user agent.

TYPE: GetVersionCommand

get_browser_command_line staticmethod

get_browser_command_line()

Returns the command line switches for the browser process.

RETURNS DESCRIPTION
GetBrowserCommandLineCommand

The CDP command that returns command line arguments.

TYPE: GetBrowserCommandLineCommand

Note: Only works if --enable-automation is on the command line.

get_histograms staticmethod

get_histograms(query=None, delta=False)

Get Chrome histograms.

PARAMETER DESCRIPTION
query

Requested substring in name. Only histograms which have query as a substring in their name are extracted. An empty or absent query returns all histograms.

TYPE: Optional[str] DEFAULT: None

delta

If true, retrieve delta since last delta call.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
GetHistogramsCommand

The CDP command that returns histogram data.

TYPE: GetHistogramsCommand

get_histogram staticmethod

get_histogram(name, delta=False)

Get a Chrome histogram by name.

PARAMETER DESCRIPTION
name

Requested histogram name.

TYPE: str

delta

If true, retrieve delta since last delta call.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
GetHistogramCommand

The CDP command that returns histogram data.

TYPE: GetHistogramCommand

get_window_bounds staticmethod

get_window_bounds(window_id)

Get position and size of the browser window.

PARAMETER DESCRIPTION
window_id

Browser window id.

TYPE: WindowID

RETURNS DESCRIPTION
GetWindowBoundsCommand

The CDP command that returns window bounds information.

TYPE: GetWindowBoundsCommand

get_window_for_target staticmethod

get_window_for_target(target_id=None)

Get the browser window that contains the devtools target.

PARAMETER DESCRIPTION
target_id

Devtools agent host id. If called as a part of the session, associated targetId is used.

TYPE: Optional[str] DEFAULT: None

RETURNS DESCRIPTION
GetWindowForTargetCommand

The CDP command that returns window information including windowId and bounds.

TYPE: GetWindowForTargetCommand

set_window_bounds staticmethod

set_window_bounds(window_id, bounds)

Set position and/or size of the browser window.

PARAMETER DESCRIPTION
window_id

Browser window id.

TYPE: WindowID

bounds

New window bounds. The 'minimized', 'maximized' and 'fullscreen' states cannot be combined with 'left', 'top', 'width' or 'height'. Leaves unspecified fields unchanged.

TYPE: Bounds

RETURNS DESCRIPTION
SetWindowBoundsCommand

The CDP command that sets window bounds.

TYPE: SetWindowBoundsCommand

set_contents_size staticmethod

set_contents_size(window_id, width=None, height=None)

Set size of the browser contents resizing browser window as necessary.

PARAMETER DESCRIPTION
window_id

Browser window id.

TYPE: WindowID

width

The window contents width in DIP. Assumes current width if omitted. Must be specified if 'height' is omitted.

TYPE: Optional[int] DEFAULT: None

height

The window contents height in DIP. Assumes current height if omitted. Must be specified if 'width' is omitted.

TYPE: Optional[int] DEFAULT: None

RETURNS DESCRIPTION
SetContentsSizeCommand

The CDP command that sets window contents size.

TYPE: SetContentsSizeCommand

set_dock_tile staticmethod

set_dock_tile(badge_label=None, image=None)

Set dock tile details, platform-specific.

PARAMETER DESCRIPTION
badge_label

Optional badge label.

TYPE: Optional[str] DEFAULT: None

image

Png encoded image (base64 string when passed over JSON).

TYPE: Optional[str] DEFAULT: None

RETURNS DESCRIPTION
SetDockTileCommand

The CDP command that sets dock tile details.

TYPE: SetDockTileCommand

execute_browser_command staticmethod

execute_browser_command(command_id)

Invoke custom browser commands used by telemetry.

PARAMETER DESCRIPTION
command_id

Browser command identifier.

TYPE: BrowserCommandId

RETURNS DESCRIPTION
ExecuteBrowserCommandCommand

The CDP command that executes browser command.

TYPE: ExecuteBrowserCommandCommand

add_privacy_sandbox_enrollment_override staticmethod

add_privacy_sandbox_enrollment_override(url)

Allows a site to use privacy sandbox features that require enrollment without the site actually being enrolled. Only supported on page targets.

PARAMETER DESCRIPTION
url

Site URL.

TYPE: str

RETURNS DESCRIPTION
AddPrivacySandboxEnrollmentOverrideCommand

The CDP command that adds enrollment

TYPE: AddPrivacySandboxEnrollmentOverrideCommand

AddPrivacySandboxEnrollmentOverrideCommand

override.

add_privacy_sandbox_coordinator_key_config staticmethod

add_privacy_sandbox_coordinator_key_config(api, coordinator_origin, key_config, browser_context_id=None)

Configures encryption keys used with a given privacy sandbox API to talk to a trusted coordinator. Since this is intended for test automation only, coordinatorOrigin must be a .test domain. No existing coordinator configuration for the origin may exist.

PARAMETER DESCRIPTION
api

Privacy Sandbox API type.

TYPE: PrivacySandboxAPI

coordinator_origin

Coordinator origin (must be .test domain).

TYPE: str

key_config

Key configuration string.

TYPE: str

browser_context_id

BrowserContext to perform the action in. When omitted, default browser context is used.

TYPE: Optional[BrowserContextID] DEFAULT: None

RETURNS DESCRIPTION
AddPrivacySandboxCoordinatorKeyConfigCommand

The CDP command that adds key config.

TYPE: AddPrivacySandboxCoordinatorKeyConfigCommand

set_permission staticmethod

set_permission(permission, setting, origin=None, browser_context_id=None)

Set permission settings for given origin.

PARAMETER DESCRIPTION
permission

Descriptor of permission to override.

TYPE: PermissionDescriptor

setting

Setting of the permission.

TYPE: PermissionSetting

origin

Origin the permission applies to, all origins if not specified.

TYPE: Optional[str] DEFAULT: None

browser_context_id

Context to override. When omitted, default browser context is used.

TYPE: Optional[BrowserContextID] DEFAULT: None

RETURNS DESCRIPTION
SetPermissionCommand

The CDP command that sets permission.

TYPE: SetPermissionCommand

grant_permissions staticmethod

grant_permissions(permissions, origin=None, browser_context_id=None)

Grant specific permissions to the given origin and reject all others.

PARAMETER DESCRIPTION
permissions

List of permissions to grant.

TYPE: list[PermissionType]

origin

Origin the permission applies to, all origins if not specified.

TYPE: Optional[str] DEFAULT: None

browser_context_id

BrowserContext to override permissions. When omitted, default browser context is used.

TYPE: Optional[BrowserContextID] DEFAULT: None

RETURNS DESCRIPTION
GrantPermissionsCommand

The CDP command that grants permissions.

TYPE: GrantPermissionsCommand

reset_permissions staticmethod

reset_permissions(browser_context_id=None)

Reset all permission management for all origins.

PARAMETER DESCRIPTION
browser_context_id

BrowserContext to reset permissions. When omitted, default browser context is used.

TYPE: Optional[BrowserContextID] DEFAULT: None

RETURNS DESCRIPTION
ResetPermissionsCommand

The CDP command that resets permissions.

TYPE: ResetPermissionsCommand

set_download_behavior staticmethod

set_download_behavior(behavior, browser_context_id=None, download_path=None, events_enabled=False)

Set the behavior when downloading a file.

PARAMETER DESCRIPTION
behavior

Whether to allow all or deny all download requests, or use default Chrome behavior if available (otherwise deny). allowAndName allows download and names files according to their download guids.

TYPE: DownloadBehavior

browser_context_id

BrowserContext to set download behavior. When omitted, default browser context is used.

TYPE: Optional[BrowserContextID] DEFAULT: None

download_path

The default path to save downloaded files to. This is required if behavior is set to 'allow' or 'allowAndName'.

TYPE: Optional[str] DEFAULT: None

events_enabled

Whether to emit download events (defaults to false).

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
SetDownloadBehaviorCommand

The CDP command that sets download behavior.

TYPE: SetDownloadBehaviorCommand

cancel_download staticmethod

cancel_download(guid, browser_context_id=None)

Cancel a download if in progress.

PARAMETER DESCRIPTION
guid

Global unique identifier of the download.

TYPE: str

browser_context_id

BrowserContext to perform the action in. When omitted, default browser context is used.

TYPE: Optional[BrowserContextID] DEFAULT: None

RETURNS DESCRIPTION
CancelDownloadCommand

The CDP command that cancels download.

TYPE: CancelDownloadCommand

close staticmethod

close()

Close browser gracefully.

RETURNS DESCRIPTION
CloseCommand

The CDP command that closes the browser.

TYPE: CloseCommand

crash staticmethod

crash()

Crashes browser on the main thread.

RETURNS DESCRIPTION
CrashCommand

The CDP command that crashes the browser.

TYPE: CrashCommand

crash_gpu_process staticmethod

crash_gpu_process()

Crashes GPU process.

RETURNS DESCRIPTION
CrashGpuProcessCommand

The CDP command that crashes the GPU process.

TYPE: CrashGpuProcessCommand

set_window_maximized staticmethod

set_window_maximized(window_id)

Maximize a browser window.

PARAMETER DESCRIPTION
window_id

Browser window id.

TYPE: WindowID

RETURNS DESCRIPTION
SetWindowBoundsCommand

The CDP command that maximizes the window.

TYPE: SetWindowBoundsCommand

set_window_minimized staticmethod

set_window_minimized(window_id)

Minimize a browser window.

PARAMETER DESCRIPTION
window_id

Browser window id.

TYPE: WindowID

RETURNS DESCRIPTION
SetWindowBoundsCommand

The CDP command that minimizes the window.

TYPE: SetWindowBoundsCommand

set_window_fullscreen staticmethod

set_window_fullscreen(window_id)

Set a browser window to fullscreen.

PARAMETER DESCRIPTION
window_id

Browser window id.

TYPE: WindowID

RETURNS DESCRIPTION
SetWindowBoundsCommand

The CDP command that sets window to fullscreen.

TYPE: SetWindowBoundsCommand

set_window_normal staticmethod

set_window_normal(window_id)

Set a browser window to normal state.

PARAMETER DESCRIPTION
window_id

Browser window id.

TYPE: WindowID

RETURNS DESCRIPTION
SetWindowBoundsCommand

The CDP command that sets window to normal state.

TYPE: SetWindowBoundsCommand

用法

浏览器命令通常由浏览器类在内部使用,用于管理浏览器实例:

from pydoll.commands.browser_commands import get_version
from pydoll.connection.connection_handler import ConnectionHandler

# Get browser version information
connection = ConnectionHandler()
version_info = await get_version(connection)

可用命令

浏览器命令模块提供以下功能:

  • 获取浏览器版本和用户代理信息
  • 管理浏览器目标(标签页、窗口)
  • 控制浏览器范围的设置和权限
  • 处理浏览器生命周期事件

Internal Usage

These commands are primarily used internally by the Chrome and Edge browser classes. Direct usage is recommended only for advanced scenarios.