Skip to contents

Find HTML child elements from elements in a collection. Provides a convenient way to operate on a collection of elements.

find_each_element() finds the first child element of each element in the collection.

find_all_elements() finds every child element of every element in the collection.

Usage

find_each_element(
  x,
  css = NULL,
  xpath = NULL,
  id = NULL,
  class_name = NULL,
  name = NULL
)

find_all_elements(
  x,
  css = NULL,
  xpath = NULL,
  id = NULL,
  class_name = NULL,
  name = NULL
)

Arguments

x

A selenider_elements object.

css

A CSS selector.

xpath

An XPath.

id

The id of the elements you want to select.

class_name

The class name of the elements you want to select.

name

The name attribute of the elements you want to select.

Value

A selenider_elements object.

Details

find_each_element() will usually preserve the length of the input, since for each element in the collection, one new element will be found. However, if an element in the collection cannot be found, it will not be included in the resulting collection.

find_each_element(x, ...) is roughly equivalent to:

x |>
  as.list() |>
  lapply(\(x) find_element(x, ...)) |>
  elem_flatten()

Similarly, find_all_elements(x, ...) is roughly equivalent to:

x |>
  as.list() |>
  lapply(\(x) find_elements(x, ...)) |>
  elem_flatten()

See also

  • as.list() to iterate over an element collection.

  • elem_flatten() to combine multiple selenider_element/selenider_elements objects into a single object.

Examples

html <- "
<div id='div1'>
  <p>Text 1</p>
  <button>Button 1</button>
</div>
<div id='div2'>
  <p>Text 2</p>
</div>
<div id='div3'>
  <p>Text 3</p>
</div>
<div id='div4'>
  <p>Text 4</p>
</div>
"

session <- minimal_selenider_session(html)

divs <- ss("div")

# Get the <p> tag inside each div.
divs |>
  find_each_element("p")


# Get the <button> tag in the first div as well.
divs |>
  find_all_elements("*")