跳转至

浏览器命令

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

概述

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

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
Command[GetVersionResponse]

Command[GetVersionResponse]: The CDP command that returns browser version details including protocol version, product name, revision, and user agent.

reset_permissions staticmethod

reset_permissions(browser_context_id=None)

Generates a command to reset all permissions.

PARAMETER DESCRIPTION
browser_context_id

The browser context to reset permissions for. If not specified, resets permissions for the default context.

TYPE: Optional[str] DEFAULT: None

RETURNS DESCRIPTION
Command[Response]

Command[Response]: The CDP command that returns a basic success response.

cancel_download staticmethod

cancel_download(guid, browser_context_id=None)

Generates a command to cancel a download.

PARAMETER DESCRIPTION
guid

Global unique identifier of the download.

TYPE: str

browser_context_id

The browser context the download belongs to. If not specified, uses the default context.

TYPE: Optional[str] DEFAULT: None

RETURNS DESCRIPTION
Command[Response]

Command[Response]: The CDP command that returns a basic success response.

crash staticmethod

crash()

Generates a command to crash the browser main process.

RETURNS DESCRIPTION
Command[Response]

Command[Response]: The CDP command that returns a basic success response before crashing the browser.

crash_gpu_process staticmethod

crash_gpu_process()

Generates a command to crash the browser GPU process.

RETURNS DESCRIPTION
Command[Response]

Command[Response]: The CDP command that returns a basic success response before crashing the GPU process.

set_download_behavior staticmethod

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

Generates a command to set the download behavior for the browser.

PARAMETER DESCRIPTION
behavior

The behavior to set for downloads.

TYPE: DownloadBehavior

download_path

The path to set for downloads.

TYPE: Optional[str] DEFAULT: None

RETURNS DESCRIPTION
Command[Response]

Command[Response]: The CDP command that returns a basic success response after setting the download path.

close staticmethod

close()

Generates a command to close the browser.

RETURNS DESCRIPTION
Command[Response]

Command[Response]: The CDP command that returns a basic success response before closing the browser.

get_window_for_target staticmethod

get_window_for_target(target_id)

Generates a command to get the window for a given target ID.

PARAMETER DESCRIPTION
target_id

The target_id to get the window for.

TYPE: str

RETURNS DESCRIPTION
Command[GetWindowForTargetResponse]

Command[GetWindowForTargetResponse]: The CDP command that returns window information including windowId and bounds.

set_window_bounds staticmethod

set_window_bounds(window_id, bounds)

Generates a command to set the bounds of a window.

PARAMETER DESCRIPTION
window_id

The ID of the window to set the bounds for.

TYPE: int

bounds

The bounds to set for the window, which should include windowState and optionally width, height, x, and y coordinates.

TYPE: WindowBoundsDict

RETURNS DESCRIPTION
Command[Response]

Command[Response]: The CDP command that returns a basic success response after setting the window bounds.

set_window_maximized staticmethod

set_window_maximized(window_id)

Generates a command to maximize a window.

PARAMETER DESCRIPTION
window_id

The ID of the window to maximize.

TYPE: int

RETURNS DESCRIPTION
Command[Response]

Command[Response]: The CDP command that returns a basic success response after maximizing the window.

set_window_minimized staticmethod

set_window_minimized(window_id)

Generates a command to minimize a window.

PARAMETER DESCRIPTION
window_id

The ID of the window to minimize.

TYPE: int

RETURNS DESCRIPTION
Command[Response]

Command[Response]: The CDP command that returns a basic success response after minimizing the window.

grant_permissions staticmethod

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

Generates a command to grant specific permissions to the given origin.

PARAMETER DESCRIPTION
permissions

list of permissions to grant. See PermissionType enum for available permissions.

TYPE: list[PermissionType]

origin

The origin to grant permissions to. If not specified, grants for all origins.

TYPE: Optional[str] DEFAULT: None

browser_context_id

The browser context to grant permissions in. If not specified, uses the default context.

TYPE: Optional[str] DEFAULT: None

RETURNS DESCRIPTION
Command[Response]

Command[Response]: The CDP command that returns a basic success response after granting the specified permissions.

用法

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

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.