Skip to contents

Operators to extract a subset of elements, or a single element, from a selenider element collection.

elem_filter() and elem_find() allow you to use conditions to filter HTML elements (see is_present() and other conditions). elem_find() returns the first element that satisfies one or more conditions, while elem_filter() returns every element that satisfies these conditions.

[ and [[ with a numeric subscript can be used on an element collection to filter the elements by position. [ returns a single element at a specified location, while [[ returns a collection of the elements at more than one position.

Usage

elem_filter(x, ...)

elem_find(x, ...)

# S3 method for selenider_elements
[(x, i)

# S3 method for selenider_elements
[[(x, i)

Arguments

x

A selenider_elements object.

...

<dynamic-dots> Conditions (functions or function calls) that are used to filter the elements of x.

i

A number (or for [, a vector of one or more numbers) used to select elements by position.

Value

elem_filter() and [ return a selenider_elements object, since they can result in multiple elements. elem_find() and [[ return a single selenider_element object.

Details

As with the find_element() and find_elements() functions, these functions are lazy, meaning that the elements are not fetched and filtered until they are needed.

Conditions can be functions or function calls (see elem_expect() for more details).

See also

  • find_elements() and ss() to get elements to filter.

  • is_present() and other conditions for predicates for HTML elements. (If you scroll down to the See also section, you will find the rest).

Examples

html <- "
<button disabled>Button 1</button>
<button>Button 2</button>
<p>Text</p>
<div style='display:none;'></div>
"
session <- minimal_selenider_session(html)

elements <- ss("*")

# Gives the same result as s()
elements[[1]]

elements[1:3]

elements[-2]

elements |>
  elem_filter(is_visible)

elements |>
  elem_find(is_visible)

# The above is equivalent to:
visible_elems <- elements |>
  elem_filter(is_visible)
visible_elems[[1]]

# In R >= 4.3.0, we can instead do:
# ss(".class1") |>
#   elem_filter(is_visible) |>
#   _[[1]]

ss("button") |>
  elem_filter(is_enabled)