CSS

This domain exposes CSS read/write operations. All CSS objects (stylesheets, rules, and styles) have an associated id used in subsequent operations on the related object. Each object type has a specific id structure, and those are not interchangeable between objects of different kinds. CSS objects can be loaded using the get*ForNode() calls (which accept a DOM node id). A client can also keep track of stylesheets via the styleSheetAdded/styleSheetRemoved events and subsequently load the required stylesheet contents using the getStyleSheet[Text]() methods.

This CDP domain is experimental.

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.css.StyleSheetId
class cdp.css.StyleSheetOrigin

Stylesheet type: “injected” for stylesheets injected via extension, “user-agent” for user-agent stylesheets, “inspector” for stylesheets created by the inspector (i.e. those holding the “via inspector” rules), “regular” for regular stylesheets.

INJECTED = 'injected'
INSPECTOR = 'inspector'
REGULAR = 'regular'
USER_AGENT = 'user-agent'
class cdp.css.PseudoElementMatches(pseudo_type, matches)

CSS rule collection for a single pseudo style.

matches = None

Matches of CSS rules applicable to the pseudo style.

pseudo_type = None

Pseudo element type.

class cdp.css.InheritedStyleEntry(matched_css_rules, inline_style=None)

Inherited CSS rule collection from ancestor node.

inline_style = None

The ancestor node’s inline style, if any, in the style inheritance chain.

matched_css_rules = None

Matches of CSS rules matching the ancestor node in the style inheritance chain.

class cdp.css.RuleMatch(rule, matching_selectors)

Match data for a CSS rule.

matching_selectors = None

Matching selector indices in the rule’s selectorList selectors (0-based).

rule = None

CSS rule in the match.

class cdp.css.Value(text, range_=None)

Data for a simple selector (these are delimited by commas in a selector list).

range_ = None

Value range in the underlying resource (if available).

text = None

Value text.

class cdp.css.SelectorList(selectors, text)

Selector list data.

selectors = None

Selectors in the list.

text = None

Rule selector text.

class cdp.css.CSSStyleSheetHeader(style_sheet_id, frame_id, source_url, origin, title, disabled, is_inline, start_line, start_column, length, source_map_url=None, owner_node=None, has_source_url=None)

CSS stylesheet metainformation.

disabled = None

Denotes whether the stylesheet is disabled.

frame_id = None

Owner frame identifier.

has_source_url = None

Whether the sourceURL field value comes from the sourceURL comment.

is_inline = None

Whether this stylesheet is created for STYLE tag by parser. This flag is not set for document.written STYLE tags.

length = None

Size of the content (in characters).

origin = None

Stylesheet origin.

owner_node = None

The backend id for the owner node of the stylesheet.

source_map_url = None

URL of source map associated with the stylesheet (if any).

source_url = None

Stylesheet resource URL.

start_column = None

Column offset of the stylesheet within the resource (zero based).

start_line = None

Line offset of the stylesheet within the resource (zero based).

style_sheet_id = None

The stylesheet identifier.

title = None

Stylesheet title.

class cdp.css.CSSRule(selector_list, origin, style, style_sheet_id=None, media=None)

CSS rule representation.

media = None

Media list array (for rules involving media queries). The array enumerates media queries starting with the innermost one, going outwards.

origin = None

Parent stylesheet’s origin.

selector_list = None

Rule selector data.

style = None

Associated style declaration.

style_sheet_id = None

The css style sheet identifier (absent for user agent stylesheet and user-specified stylesheet rules) this rule came from.

class cdp.css.RuleUsage(style_sheet_id, start_offset, end_offset, used)

CSS coverage information.

end_offset = None

Offset of the end of the rule body from the beginning of the stylesheet.

start_offset = None

Offset of the start of the rule (including selector) from the beginning of the stylesheet.

style_sheet_id = None

The css style sheet identifier (absent for user agent stylesheet and user-specified stylesheet rules) this rule came from.

used = None

Indicates whether the rule was actually used by some element in the page.

class cdp.css.SourceRange(start_line, start_column, end_line, end_column)

Text range within a resource. All numbers are zero-based.

end_column = None

End column of range (exclusive).

end_line = None

End line of range

start_column = None

Start column of range (inclusive).

start_line = None

Start line of range.

class cdp.css.ShorthandEntry(name, value, important=None)
important = None

Whether the property has “!important” annotation (implies false if absent).

name = None

Shorthand name.

value = None

Shorthand value.

class cdp.css.CSSComputedStyleProperty(name, value)
name = None

Computed style property name.

value = None

Computed style property value.

class cdp.css.CSSStyle(css_properties, shorthand_entries, style_sheet_id=None, css_text=None, range_=None)

CSS style representation.

css_properties = None

CSS properties in the style.

css_text = None

Style declaration text (if available).

range_ = None

Style declaration range in the enclosing stylesheet (if available).

shorthand_entries = None

Computed values for all shorthands found in the style.

style_sheet_id = None

The css style sheet identifier (absent for user agent stylesheet and user-specified stylesheet rules) this rule came from.

class cdp.css.CSSProperty(name, value, important=None, implicit=None, text=None, parsed_ok=None, disabled=None, range_=None)

CSS property declaration data.

disabled = None

Whether the property is disabled by the user (present for source-based properties only).

implicit = None

Whether the property is implicit (implies false if absent).

important = None

Whether the property has “!important” annotation (implies false if absent).

name = None

The property name.

parsed_ok = None

Whether the property is understood by the browser (implies true if absent).

range_ = None

The entire property range in the enclosing style declaration (if available).

text = None

The full property text as specified in the style.

value = None

The property value.

class cdp.css.CSSMedia(text, source, source_url=None, range_=None, style_sheet_id=None, media_list=None)

CSS media rule descriptor.

media_list = None

Array of media queries.

range_ = None

The associated rule (@media or @import) header range in the enclosing stylesheet (if available).

source = None

Source of the media query: “mediaRule” if specified by a @media rule, “importRule” if specified by an @import rule, “linkedSheet” if specified by a “media” attribute in a linked stylesheet’s LINK tag, “inlineSheet” if specified by a “media” attribute in an inline stylesheet’s STYLE tag.

source_url = None

URL of the document containing the media query description.

style_sheet_id = None

Identifier of the stylesheet containing this object (if exists).

text = None

Media query text.

class cdp.css.MediaQuery(expressions, active)

Media query descriptor.

active = None

Whether the media query condition is satisfied.

expressions = None

Array of media query expressions.

class cdp.css.MediaQueryExpression(value, unit, feature, value_range=None, computed_length=None)

Media query expression descriptor.

computed_length = None

Computed length of media query expression (if applicable).

feature = None

Media query expression feature.

unit = None

Media query expression units.

value = None

Media query expression value.

value_range = None

The associated range of the value text in the enclosing stylesheet (if available).

class cdp.css.PlatformFontUsage(family_name, is_custom_font, glyph_count)

Information about amount of glyphs that were rendered with given font.

family_name = None

Font’s family name reported by platform.

glyph_count = None

Amount of glyphs that were rendered with this font.

is_custom_font = None

Indicates if the font was downloaded or resolved locally.

class cdp.css.FontFace(font_family, font_style, font_variant, font_weight, font_stretch, unicode_range, src, platform_font_family)

Properties of a web font: https://www.w3.org/TR/2008/REC-CSS2-20080411/fonts.html#font-descriptions

font_family = None

The font-family.

font_stretch = None

The font-stretch.

font_style = None

The font-style.

font_variant = None

The font-variant.

font_weight = None

The font-weight.

platform_font_family = None

The resolved platform font family

src = None

The src.

unicode_range = None

The unicode-range.

class cdp.css.CSSKeyframesRule(animation_name, keyframes)

CSS keyframes rule representation.

animation_name = None

Animation name.

keyframes = None

List of keyframes.

class cdp.css.CSSKeyframeRule(origin, key_text, style, style_sheet_id=None)

CSS keyframe rule representation.

key_text = None

Associated key text.

origin = None

Parent stylesheet’s origin.

style = None

Associated style declaration.

style_sheet_id = None

The css style sheet identifier (absent for user agent stylesheet and user-specified stylesheet rules) this rule came from.

class cdp.css.StyleDeclarationEdit(style_sheet_id, range_, text)

A descriptor of operation to mutate style declaration text.

range_ = None

The range of the style text in the enclosing stylesheet.

style_sheet_id = None

The css style sheet identifier.

text = None

New style text.

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.css.add_rule(style_sheet_id, rule_text, location)

Inserts a new rule with the given ruleText in a stylesheet with given styleSheetId, at the position specified by location.

Parameters:
  • style_sheet_id (StyleSheetId) – The css style sheet identifier where a new rule should be inserted.
  • rule_text (str) – The text of a new rule.
  • location (SourceRange) – Text position of a new rule in the target style sheet.
Return type:

Generator[Dict[str, Any], Dict[str, Any], CSSRule]

Returns:

The newly created rule.

cdp.css.collect_class_names(style_sheet_id)

Returns all class names from specified stylesheet.

Parameters:style_sheet_id (StyleSheetId) –
Return type:Generator[Dict[str, Any], Dict[str, Any], List[str]]
Returns:Class name list.
cdp.css.create_style_sheet(frame_id)

Creates a new special “via-inspector” stylesheet in the frame with given frameId.

Parameters:frame_id (FrameId) – Identifier of the frame where “via-inspector” stylesheet should be created.
Return type:Generator[Dict[str, Any], Dict[str, Any], StyleSheetId]
Returns:Identifier of the created “via-inspector” stylesheet.
cdp.css.disable()

Disables the CSS agent for the given page.

Return type:Generator[Dict[str, Any], Dict[str, Any], None]
cdp.css.enable()

Enables the CSS agent for the given page. Clients should not assume that the CSS agent has been enabled until the result of this command is received.

Return type:Generator[Dict[str, Any], Dict[str, Any], None]
cdp.css.force_pseudo_state(node_id, forced_pseudo_classes)

Ensures that the given node will have specified pseudo-classes whenever its style is computed by the browser.

Parameters:
  • node_id (NodeId) – The element id for which to force the pseudo state.
  • forced_pseudo_classes (List[str]) – Element pseudo classes to force when computing the element’s style.
Return type:

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

cdp.css.get_background_colors(node_id)
Parameters:node_id (NodeId) – Id of the node to get background colors for.
Return type:Generator[Dict[str, Any], Dict[str, Any], Tuple[Optional[List[str]], Optional[str], Optional[str]]]
Returns:A tuple with the following items:
  1. backgroundColors - (Optional) The range of background colors behind this element, if it contains any visible text. If no visible text is present, this will be undefined. In the case of a flat background color, this will consist of simply that color. In the case of a gradient, this will consist of each of the color stops. For anything more complicated, this will be an empty array. Images will be ignored (as if the image had failed to load).
  2. computedFontSize - (Optional) The computed font size for this node, as a CSS computed value string (e.g. ‘12px’).
  3. computedFontWeight - (Optional) The computed font weight for this node, as a CSS computed value string (e.g. ‘normal’ or ‘100’).
cdp.css.get_computed_style_for_node(node_id)

Returns the computed style for a DOM node identified by nodeId.

Parameters:node_id (NodeId) –
Return type:Generator[Dict[str, Any], Dict[str, Any], List[CSSComputedStyleProperty]]
Returns:Computed style for the specified DOM node.
cdp.css.get_inline_styles_for_node(node_id)

Returns the styles defined inline (explicitly in the “style” attribute and implicitly, using DOM attributes) for a DOM node identified by nodeId.

Parameters:node_id (NodeId) –
Return type:Generator[Dict[str, Any], Dict[str, Any], Tuple[Optional[CSSStyle], Optional[CSSStyle]]]
Returns:A tuple with the following items:
  1. inlineStyle - (Optional) Inline style for the specified DOM node.
  2. attributesStyle - (Optional) Attribute-defined element style (e.g. resulting from “width=20 height=100%”).
cdp.css.get_matched_styles_for_node(node_id)

Returns requested styles for a DOM node identified by nodeId.

Parameters:node_id (NodeId) –
Return type:Generator[Dict[str, Any], Dict[str, Any], Tuple[Optional[CSSStyle], Optional[CSSStyle], Optional[List[RuleMatch]], Optional[List[PseudoElementMatches]], Optional[List[InheritedStyleEntry]], Optional[List[CSSKeyframesRule]]]]
Returns:A tuple with the following items:
  1. inlineStyle - (Optional) Inline style for the specified DOM node.
  2. attributesStyle - (Optional) Attribute-defined element style (e.g. resulting from “width=20 height=100%”).
  3. matchedCSSRules - (Optional) CSS rules matching this node, from all applicable stylesheets.
  4. pseudoElements - (Optional) Pseudo style matches for this node.
  5. inherited - (Optional) A chain of inherited styles (from the immediate node parent up to the DOM tree root).
  6. cssKeyframesRules - (Optional) A list of CSS keyframed animations matching this node.
cdp.css.get_media_queries()

Returns all media queries parsed by the rendering engine.

Return type:Generator[Dict[str, Any], Dict[str, Any], List[CSSMedia]]
Returns:
cdp.css.get_platform_fonts_for_node(node_id)

Requests information about platform fonts which we used to render child TextNodes in the given node.

Parameters:node_id (NodeId) –
Return type:Generator[Dict[str, Any], Dict[str, Any], List[PlatformFontUsage]]
Returns:Usage statistics for every employed platform font.
cdp.css.get_style_sheet_text(style_sheet_id)

Returns the current textual content for a stylesheet.

Parameters:style_sheet_id (StyleSheetId) –
Return type:Generator[Dict[str, Any], Dict[str, Any], str]
Returns:The stylesheet text.
cdp.css.set_effective_property_value_for_node(node_id, property_name, value)

Find a rule with the given active property for the given node and set the new value for this property

Parameters:
  • node_id (NodeId) – The element id for which to set property.
  • property_name (str) –
  • value (str) –
Return type:

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

cdp.css.set_keyframe_key(style_sheet_id, range_, key_text)

Modifies the keyframe rule key text.

Parameters:
  • style_sheet_id (StyleSheetId) –
  • range
  • key_text (str) –
Return type:

Generator[Dict[str, Any], Dict[str, Any], Value]

Returns:

The resulting key text after modification.

cdp.css.set_media_text(style_sheet_id, range_, text)

Modifies the rule selector.

Parameters:
  • style_sheet_id (StyleSheetId) –
  • range
  • text (str) –
Return type:

Generator[Dict[str, Any], Dict[str, Any], CSSMedia]

Returns:

The resulting CSS media rule after modification.

cdp.css.set_rule_selector(style_sheet_id, range_, selector)

Modifies the rule selector.

Parameters:
  • style_sheet_id (StyleSheetId) –
  • range
  • selector (str) –
Return type:

Generator[Dict[str, Any], Dict[str, Any], SelectorList]

Returns:

The resulting selector list after modification.

cdp.css.set_style_sheet_text(style_sheet_id, text)

Sets the new stylesheet text.

Parameters:
Return type:

Generator[Dict[str, Any], Dict[str, Any], Optional[str]]

Returns:

(Optional) URL of source map associated with script (if any).

cdp.css.set_style_texts(edits)

Applies specified style edits one after another in the given order.

Parameters:edits (List[StyleDeclarationEdit]) –
Return type:Generator[Dict[str, Any], Dict[str, Any], List[CSSStyle]]
Returns:The resulting styles after modification.
cdp.css.start_rule_usage_tracking()

Enables the selector recording.

Return type:Generator[Dict[str, Any], Dict[str, Any], None]
cdp.css.stop_rule_usage_tracking()

Stop tracking rule usage and return the list of rules that were used since last call to takeCoverageDelta (or since start of coverage instrumentation)

Return type:Generator[Dict[str, Any], Dict[str, Any], List[RuleUsage]]
Returns:
cdp.css.take_coverage_delta()

Obtain list of rules that became used since last call to this method (or since start of coverage instrumentation)

Return type:Generator[Dict[str, Any], Dict[str, Any], List[RuleUsage]]
Returns:

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.css.FontsUpdated(font)

Fires whenever a web font is updated. A non-empty font parameter indicates a successfully loaded web font

font = None

The web font that has loaded.

class cdp.css.MediaQueryResultChanged

Fires whenever a MediaQuery result changes (for example, after a browser window has been resized.) The current implementation considers only viewport-dependent media features.

class cdp.css.StyleSheetAdded(header)

Fired whenever an active document stylesheet is added.

header = None

Added stylesheet metainfo.

class cdp.css.StyleSheetChanged(style_sheet_id)

Fired whenever a stylesheet is changed as a result of the client operation.

class cdp.css.StyleSheetRemoved(style_sheet_id)

Fired whenever an active document stylesheet is removed.

style_sheet_id = None

Identifier of the removed stylesheet.