capybara.selector package

Submodules

capybara.selector.abstract_filter module

class capybara.selector.abstract_filter.AbstractFilter(name, func, boolean=None, default=None, skip_if=None, valid_values=None)[source]

Bases: object

A rule to apply to identify desired nodes.

Parameters:
  • name (str) – The name of this filter.
  • func (Callable[[Element, Any], bool]) – A function that determines whether a given node matches a desired value.
  • boolean (bool, optional) – Whether the filter evaluates boolean values. Defaults to False.
  • default (object, optional) – A default desired value, if any. Defaults to None.
  • skip_if (object, optional) – A value which, if provided, signifies that this rule should be skipped.
  • valid_values (List[Any], optional) – The values the filter supports.
has_default

bool – Whether this rule has a default desired value.

has_skip_if

bool – Whether this rule has a value for which it should be skipped.

skip(value)[source]

Returns whether this rule should be skipped for the given value.

Parameters:value (Any) – A value which may be used to match nodes.
Returns:Whether this rule should be skipped.
Return type:bool

capybara.selector.expression_filter module

class capybara.selector.expression_filter.ExpressionFilter(name, func, boolean=None, default=None, skip_if=None, valid_values=None)[source]

Bases: capybara.selector.abstract_filter.AbstractFilter

apply_filter(expr, value)[source]

Returns the given expression filtered by the given value.

Parameters:
  • expr (xpath.expression.AbstractExpression) – The expression to filter.
  • value (object) – The desired value with which the expression should be filtered.
Returns:

The filtered expression.

Return type:

xpath.expression.AbstractExpression

capybara.selector.filter_set module

class capybara.selector.filter_set.FilterSet(name, descriptions=None, filters=None)[source]

Bases: object

A group of filters for use by Selector objects.

Parameters:
  • name (str) – The name of this set of filters.
  • descriptions (List[Callable[[Dict[str, Any]], str]]) – Functions that generate descriptions for options.
  • filters (Dict[str, AbstractFilter], optional) – The filters in this set. Defaults to {}.
description(options)[source]

Returns a description of the given filter options relevant to this filter set.

Parameters:options (Dict[str, Any]) – The filter options to describe.
Returns:A description of the filter options.
Return type:str
class capybara.selector.filter_set.FilterSetFactory(name)[source]

Bases: object

A factory for configuring and building FilterSet instances.

build_filter_set()[source]

FilterSet: Returns a new FilterSet with the current factory config.

describe(func)[source]

Decorates a function that builds a description of some filter set options.

Parameters:func (Callable[[Dict[str, Any]], str]) – The description builder function.
expression_filter(name, **kwargs)[source]
node_filter(name, **kwargs)[source]

Returns a decorator function for adding node filters to built sets.

Parameters:
  • name (str) – The name of the filter.
  • **kwargs – Variable keyword arguments for the filter.
Returns:

A decorator function for adding filters to

built sets.

Return type:

Callable[[Callable[[Element, Any], bool]]]

capybara.selector.filter_set.add_filter_set(name)[source]

Builds and registers a global FilterSet.

Parameters:name (str) – The name of the set.
Yields:FilterSetFactory – A configurable factory for building a FilterSet.
capybara.selector.filter_set.remove_filter_set(name)[source]

Unregisters filter set with the given name.

Parameters:name (str) – The name of the filter set.

capybara.selector.node_filter module

class capybara.selector.node_filter.NodeFilter(name, func, boolean=None, default=None, skip_if=None, valid_values=None)[source]

Bases: capybara.selector.abstract_filter.AbstractFilter

matches(node, value)[source]

Returns whether the given node matches the filter rule with the given value.

Parameters:
  • node (Element) – The node to filter.
  • value (object) – The desired value with which the node should be evaluated.
Returns:

Whether the given node matches.

Return type:

bool

capybara.selector.selector module

class capybara.selector.selector.Selector(name, label=None, descriptions=None, css=None, xpath=None, filters=None)[source]

Bases: object

A callable object used for selecting elements in a document or node.

Parameters:
  • name (str) – The name of the selector.
  • label (str, optional) – The label to use when describing this selector.
  • descriptions (List[Callable[[Dict[str, Any]], str]]) – Functions that build a description of the given filter options.
  • css (Callable[[str], str], optional) – A function to generate a CSS selector given a locator string.
  • xpath (Callable[[str], str], optional) – A function to generate an XPath query given a locator string.
  • filters (Dict[str, AbstractFilter]) – A dictionary of filters this selector should use to identify matching elements. Defaults to {}.
description(options)[source]

Returns a description of the given filter options relevant to this selector.

Parameters:options (Dict[str, Any]) – The filter options to describe.
Returns:A description of the filter options.
Return type:str
expression_filters

Dict[str, ExpressionFilter] – Returns the expression filters for this selector.

node_filters

Dict[str, NodeFilter] – Returns the node filters for this selector.

class capybara.selector.selector.SelectorFactory(name)[source]

Bases: object

A factory for configuring and building Selector instances.

Parameters:name (str) – The name of the selector.
build_selector()[source]

Selector: Returns a new Selector instance with the current configuration.

css

Sets the given function as the CSS selector generation function.

Parameters:func (Callable[[str], str]) – The CSS selector generation function.
describe(func)[source]

Decorates a function that builds a description of some selector options.

Parameters:func (Callable[[Dict[str, Any]], str]) – The description builder function.
expression_filter(name, **kwargs)[source]

Returns a decorator function for adding an expression filter.

Parameters:
  • name (str) – The name of the filter.
  • **kwargs – Variable keyword arguments for the filter.
Returns:

A decorator

function for adding an expression filter.

Return type:

Callable[[Callable[[AbstractExpression, Any], AbstractExpression]]]

filter_set(name)[source]

Adds filters from a particular global FilterSet.

Parameters:name (str) – The name of the set whose filters should be added.
node_filter(name, **kwargs)[source]

Returns a decorator function for adding a node filter.

Parameters:
  • name (str) – The name of the filter.
  • **kwargs – Variable keyword arguments for the filter.
Returns:

A decorator function for adding a node

filter.

Return type:

Callable[[Callable[[Element, Any], bool]]]

xpath

Sets the given function as the XPath query generation function.

Parameters:func (Callable[[str], str]) – The XPath query generation function.
capybara.selector.selector.add_selector(name)[source]

Builds and registers a Selector object with the given name and configuration.

Parameters:name (str) – The name of the selector.
Yields:SelectorFactory – The factory that will build the Selector.
capybara.selector.selector.remove_selector(name)[source]

Unregisters selector with the given name.

Parameters:name (str) – The name of the selector.

Module contents

capybara.selector.add_filter_set(name)[source]

Builds and registers a global FilterSet.

Parameters:name (str) – The name of the set.
Yields:FilterSetFactory – A configurable factory for building a FilterSet.
capybara.selector.add_selector(name)[source]

Builds and registers a Selector object with the given name and configuration.

Parameters:name (str) – The name of the selector.
Yields:SelectorFactory – The factory that will build the Selector.
capybara.selector.remove_filter_set(name)[source]

Unregisters filter set with the given name.

Parameters:name (str) – The name of the filter set.
capybara.selector.remove_selector(name)[source]

Unregisters selector with the given name.

Parameters:name (str) – The name of the selector.