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 TargetInfo(target_id, type_, title, url, attached, can_access_opener, opener_id=None, opener_frame_id=None, parent_frame_id=None, browser_context_id=None, subtype=None)[source]#
-
-
type_:
str# //source.chromium.org/chromium/chromium/src/+/main:content/browser/devtools/devtools_agent_host_impl.cc?ss=chromium&q=f:devtools%20-f:out%20%22::kTypeTab%5B%5D%22
- Type:
List of types
- Type:
https
-
opener_frame_id:
Optional[FrameId] = None# Frame id of originating window (is only set if target has an opener).
-
parent_frame_id:
Optional[FrameId] = None# Id of the parent frame, only present for the “iframe” targets.
-
browser_context_id:
Optional[BrowserContextID] = None#
-
type_:
- class FilterEntry(exclude=None, type_=None)[source]#
A filter used by target query/discovery/auto-attach operations.
- class TargetFilter(iterable=(), /)[source]#
The entries in TargetFilter are matched sequentially against targets and the first entry that matches determines if the target is included or not, depending on the value of
excludefield in the entry. If filter is not specified, the one assumed is [{type: “browser”, exclude: true}, {type: “tab”, exclude: true}, {}] (i.e. include everything butbrowserandtab).
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.
- attach_to_browser_target()[source]#
Attaches to the browser target, only uses flat sessionId mode.
EXPERIMENTAL
- attach_to_target(target_id, flatten=None)[source]#
Attaches to the target with given id.
- Parameters:
- Return type:
- Returns:
Id assigned to the session.
Adds the specified target to the list of targets that will be monitored for any related target creation (such as child frames, child workers and new versions of service worker) and reported through
attachedToTarget. The specified target is also auto-attached. This cancels the effect of any previoussetAutoAttachand is also cancelled by subsequentsetAutoAttach. Only available at the Browser target.EXPERIMENTAL
- Parameters:
- Return type:
- create_browser_context(dispose_on_detach=None, proxy_server=None, proxy_bypass_list=None, origins_with_universal_network_access=None)[source]#
Creates a new empty BrowserContext. Similar to an incognito profile but you can have more than one.
- Parameters:
dispose_on_detach (
Optional[bool]) – (EXPERIMENTAL) (Optional) If specified, disposes this context when debugging session disconnects.proxy_server (
Optional[str]) – (EXPERIMENTAL) (Optional) Proxy server, similar to the one passed to –proxy-serverproxy_bypass_list (
Optional[str]) – (EXPERIMENTAL) (Optional) Proxy bypass list, similar to the one passed to –proxy-bypass-listorigins_with_universal_network_access (
Optional[List[str]]) – (EXPERIMENTAL) (Optional) An optional list of origins to grant unlimited cross-origin access to. Parts of the URL other than those constituting origin are ignored.
- Return type:
- Returns:
The id of the context created.
- create_target(url, left=None, top=None, width=None, height=None, window_state=None, browser_context_id=None, enable_begin_frame_control=None, new_window=None, background=None, for_tab=None, hidden=None)[source]#
Creates a new page.
- Parameters:
url (
str) – The initial URL the page will be navigated to. An empty string indicates about:blank.left (
Optional[int]) – (EXPERIMENTAL) (Optional) Frame left origin in DIP (requires newWindow to be true or headless shell).top (
Optional[int]) – (EXPERIMENTAL) (Optional) Frame top origin in DIP (requires newWindow to be true or headless shell).width (
Optional[int]) – (Optional) Frame width in DIP (requires newWindow to be true or headless shell).height (
Optional[int]) – (Optional) Frame height in DIP (requires newWindow to be true or headless shell).window_state (
Optional[WindowState]) – (Optional) Frame window state (requires newWindow to be true or headless shell). Default is normal.browser_context_id (
Optional[BrowserContextID]) – (EXPERIMENTAL) (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 shell only, not supported on MacOS yet, false by default).new_window (
Optional[bool]) – (Optional) Whether to create a new Window or Tab (false by default, not supported by headless shell).background (
Optional[bool]) – (Optional) Whether to create the target in background or foreground (false by default, not supported by headless shell).for_tab (
Optional[bool]) – (EXPERIMENTAL) (Optional) Whether to create the target of type “tab”.hidden (
Optional[bool]) – (EXPERIMENTAL) (Optional) Whether to create a hidden target. The hidden target is observable via protocol, but not present in the tab UI strip. Cannot be created with`forTab: true``,``newWindow: true``or``background: false`. The life-time of the tab is limited to the life-time of the session.
- Return type:
- Returns:
The id of the page opened.
- dispose_browser_context(browser_context_id)[source]#
Deletes a BrowserContext. All the belonging pages will be closed without calling their beforeunload hooks.
- expose_dev_tools_protocol(target_id, binding_name=None, inherit_permissions=None)[source]#
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 following 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:
- Return type:
- get_browser_contexts()[source]#
Returns all browser contexts created with
Target.createBrowserContextmethod.
- get_targets(filter_=None)[source]#
Retrieves a list of available targets.
- Parameters:
filter – (EXPERIMENTAL) (Optional) Only targets matching filter will be reported. If filter is not specified and target discovery is currently enabled, a filter used for target discovery is used for consistency.
- Return type:
- Returns:
The list of targets.
- send_message_to_target(message, session_id=None, target_id=None)[source]#
Sends protocol message over session with given id. Consider using flat mode instead; see commands attachToTarget, setAutoAttach, and crbug.com/991325.
Deprecated since version 1.3.
- Parameters:
- Return type:
Deprecated since version 1.3.
- set_auto_attach(auto_attach, wait_for_debugger_on_start, flatten=None, filter_=None)[source]#
Controls whether to automatically attach to new targets which are considered to be directly related to this one (for example, iframes or workers). When turned on, attaches to all existing related targets as well. When turned off, automatically detaches from all currently attached targets. This also clears all targets added by
autoAttachRelatedfrom the list of targets to watch for creation of related targets. You might want to call this recursively for auto-attached targets to attach to all available targets.- 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. We plan to make this the default, deprecate non-flattened mode, and eventually retire it. See crbug.com/991325.filter – (EXPERIMENTAL) (Optional) Only targets matching filter will be attached.
- Return type:
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 AttachedToTarget(session_id, target_info, waiting_for_debugger)[source]#
EXPERIMENTAL
Issued when attached to target because of auto-attach or
attachToTargetcommand.-
target_info:
TargetInfo#
-
target_info:
- class DetachedFromTarget(session_id, target_id)[source]#
EXPERIMENTAL
Issued when detached from target for any reason (including
detachFromTargetcommand). Can be issued multiple times per target if multiple sessions have been attached to it.
- class ReceivedMessageFromTarget(session_id, message, target_id)[source]#
Notifies about a new protocol message received from the session (as reported in
attachedToTargetevent).
- class TargetCreated(target_info)[source]#
Issued when a possible inspection target is created.
-
target_info:
TargetInfo#
-
target_info:
- class TargetInfoChanged(target_info)[source]#
Issued when some information about a target has changed. This only happens between
targetCreatedandtargetDestroyed.-
target_info:
TargetInfo#
-
target_info: