Target

Supports additional targets discovery and allows to attach to them.

Types

Generally, you do not need to instantiate CDP types yourself. Instead, the API creates objects for you as return values from commands, and then you can use those objects as arguments to other commands.

class cdp.target.TargetID
class cdp.target.SessionID

Unique identifier of attached debugging session.

class cdp.target.BrowserContextID
class cdp.target.TargetInfo(target_id, type_, title, url, attached, opener_id=None, browser_context_id=None)
attached = None

Whether the target has an attached client.

browser_context_id = None
opener_id = None

Opener target Id

class cdp.target.RemoteLocation(host, port)

Commands

Each command is a generator function. The return type Generator[x, y, z] indicates that the generator yields arguments of type x, it must be resumed with an argument of type y, and it returns type z. In this library, types x and y are the same for all commands, and z is the return type you should pay attention to. For more information, see Getting Started: Commands.

cdp.target.activate_target(target_id)

Activates (focuses) the target.

Parameters:target_id (TargetID) –
Return type:Generator[Dict[str, Any], Dict[str, Any], None]
cdp.target.attach_to_browser_target()

Attaches to the browser target, only uses flat sessionId mode.

EXPERIMENTAL

Return type:Generator[Dict[str, Any], Dict[str, Any], SessionID]
Returns:Id assigned to the session.
cdp.target.attach_to_target(target_id, flatten=None)

Attaches to the target with given id.

Parameters:
  • target_id (TargetID) –
  • flatten (Optional[bool]) – (EXPERIMENTAL) (Optional) Enables “flat” access to the session via specifying sessionId attribute in the commands.
Return type:

Generator[Dict[str, Any], Dict[str, Any], SessionID]

Returns:

Id assigned to the session.

cdp.target.close_target(target_id)

Closes the target. If the target is a page that gets closed too.

Parameters:target_id (TargetID) –
Return type:Generator[Dict[str, Any], Dict[str, Any], bool]
Returns:
cdp.target.create_browser_context()

Creates a new empty BrowserContext. Similar to an incognito profile but you can have more than one.

EXPERIMENTAL

Return type:Generator[Dict[str, Any], Dict[str, Any], BrowserContextID]
Returns:The id of the context created.
cdp.target.create_target(url, width=None, height=None, browser_context_id=None, enable_begin_frame_control=None, new_window=None, background=None)

Creates a new page.

Parameters:
  • url (str) – The initial URL the page will be navigated to.
  • width (Optional[int]) – (Optional) Frame width in DIP (headless chrome only).
  • height (Optional[int]) – (Optional) Frame height in DIP (headless chrome only).
  • browser_context_id (Optional[BrowserContextID]) – (Optional) The browser context to create the page in.
  • enable_begin_frame_control (Optional[bool]) – (EXPERIMENTAL) (Optional) Whether BeginFrames for this target will be controlled via DevTools (headless chrome only, not supported on MacOS yet, false by default).
  • new_window (Optional[bool]) – (Optional) Whether to create a new Window or Tab (chrome-only, false by default).
  • background (Optional[bool]) – (Optional) Whether to create the target in background or foreground (chrome-only, false by default).
Return type:

Generator[Dict[str, Any], Dict[str, Any], TargetID]

Returns:

The id of the page opened.

cdp.target.detach_from_target(session_id=None, target_id=None)

Detaches session with given id.

Parameters:
  • session_id (Optional[SessionID]) – (Optional) Session to detach.
  • target_id (Optional[TargetID]) – (DEPRECATED) (Optional) Deprecated.
Return type:

Generator[Dict[str, Any], Dict[str, Any], None]

cdp.target.dispose_browser_context(browser_context_id)

Deletes a BrowserContext. All the belonging pages will be closed without calling their beforeunload hooks.

EXPERIMENTAL

Parameters:browser_context_id (BrowserContextID) –
Return type:Generator[Dict[str, Any], Dict[str, Any], None]
cdp.target.expose_dev_tools_protocol(target_id, binding_name=None)

Inject object to the target’s main frame that provides a communication channel with browser target.

Injected object will be available as window[bindingName].

The object has the follwing API: - binding.send(json) - a method to send messages over the remote debugging protocol - binding.onmessage = json => handleMessage(json) - a callback that will be called for the protocol notifications and command responses.

EXPERIMENTAL

Parameters:
  • target_id (TargetID) –
  • binding_name (Optional[str]) – (Optional) Binding name, ‘cdp’ if not specified.
Return type:

Generator[Dict[str, Any], Dict[str, Any], None]

cdp.target.get_browser_contexts()

Returns all browser contexts created with Target.createBrowserContext method.

EXPERIMENTAL

Return type:Generator[Dict[str, Any], Dict[str, Any], List[BrowserContextID]]
Returns:An array of browser context ids.
cdp.target.get_target_info(target_id=None)

Returns information about a target.

EXPERIMENTAL

Parameters:target_id (Optional[TargetID]) – (Optional)
Return type:Generator[Dict[str, Any], Dict[str, Any], TargetInfo]
Returns:
cdp.target.get_targets()

Retrieves a list of available targets.

Return type:Generator[Dict[str, Any], Dict[str, Any], List[TargetInfo]]
Returns:The list of targets.
cdp.target.send_message_to_target(message, session_id=None, target_id=None)

Sends protocol message over session with given id.

Parameters:
  • message (str) –
  • session_id (Optional[SessionID]) – (Optional) Identifier of the session.
  • target_id (Optional[TargetID]) – (DEPRECATED) (Optional) Deprecated.
Return type:

Generator[Dict[str, Any], Dict[str, Any], None]

cdp.target.set_auto_attach(auto_attach, wait_for_debugger_on_start, flatten=None)

Controls whether to automatically attach to new targets which are considered to be related to this one. When turned on, attaches to all existing related targets as well. When turned off, automatically detaches from all currently attached targets.

EXPERIMENTAL

Parameters:
  • auto_attach (bool) – Whether to auto-attach to related targets.
  • wait_for_debugger_on_start (bool) – Whether to pause new targets when attaching to them. Use `Runtime.runIfWaitingForDebugger` to run paused targets.
  • flatten (Optional[bool]) – (EXPERIMENTAL) (Optional) Enables “flat” access to the session via specifying sessionId attribute in the commands.
Return type:

Generator[Dict[str, Any], Dict[str, Any], None]

cdp.target.set_discover_targets(discover)

Controls whether to discover available targets and notify via targetCreated/targetInfoChanged/targetDestroyed events.

Parameters:discover (bool) – Whether to discover available targets.
Return type:Generator[Dict[str, Any], Dict[str, Any], None]
cdp.target.set_remote_locations(locations)

Enables target discovery for the specified locations, when setDiscoverTargets was set to true.

EXPERIMENTAL

Parameters:locations (List[RemoteLocation]) – List of remote locations.
Return type:Generator[Dict[str, Any], Dict[str, Any], None]

Events

Generally, you do not need to instantiate CDP events yourself. Instead, the API creates events for you and then you use the event’s attributes.

class cdp.target.AttachedToTarget(session_id, target_info, waiting_for_debugger)

EXPERIMENTAL

Issued when attached to target because of auto-attach or attachToTarget command.

session_id = None

Identifier assigned to the session used to send/receive messages.

class cdp.target.DetachedFromTarget(session_id, target_id)

EXPERIMENTAL

Issued when detached from target for any reason (including detachFromTarget command). Can be issued multiple times per target if multiple sessions have been attached to it.

session_id = None

Detached session identifier.

target_id = None

Deprecated.

class cdp.target.ReceivedMessageFromTarget(session_id, message, target_id)

Notifies about a new protocol message received from the session (as reported in attachedToTarget event).

session_id = None

Identifier of a session which sends a message.

target_id = None

Deprecated.

class cdp.target.TargetCreated(target_info)

Issued when a possible inspection target is created.

class cdp.target.TargetDestroyed(target_id)

Issued when a target is destroyed.

class cdp.target.TargetCrashed(target_id, status, error_code)

Issued when a target has crashed.

error_code = None

Termination error code.

status = None

Termination status type.

class cdp.target.TargetInfoChanged(target_info)

Issued when some information about a target has changed. This only happens between targetCreated and targetDestroyed.