Method: Capybara::Node::Matchers#has_selector?

Defined in:
lib/capybara/node/matchers.rb

#has_selector?(*args, **options, &optional_filter_block) ⇒ Boolean

Checks if a given selector is on the page or a descendant of the current node.

page.has_selector?('p#foo')
page.has_selector?(:xpath, './/p[@id="foo"]')
page.has_selector?(:foo)

By default it will check if the expression occurs at least once, but a different number can be specified.

page.has_selector?('p.foo', count: 4)

This will check if the expression occurs exactly 4 times.

It also accepts all options that Finders#all accepts, such as :text and :visible.

page.has_selector?('li', text: 'Horse', visible: true)

#has_selector? can also accept XPath expressions generated by the XPath gem:

page.has_selector?(:xpath, XPath.descendant(:p))

Parameters:

  • kind (Symbol)

    Optional selector type (:css, :xpath, :field, etc.). Defaults to default_selector.

  • locator (String)

    The locator for the specified selector

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :count (Integer) — default: nil

    Number of matching elements that should exist

  • :minimum (Integer) — default: nil

    Minimum number of matching elements that should exist

  • :maximum (Integer) — default: nil

    Maximum number of matching elements that should exist

  • :between (Range) — default: nil

    Range of number of matching elements that should exist

Returns:

  • (Boolean)

    If the expression exists


38
39
40
# File 'lib/capybara/node/matchers.rb', line 38

def has_selector?(*args, **options, &optional_filter_block)
  make_predicate(options) { assert_selector(*args, options, &optional_filter_block) }
end