🔍Find Elements

Commands to find elements within the context of another element.

The Element class provides 5 main ways to find elements:

  • contains get a single element by TEXT

  • find find a list of elements by CSS

  • findx find a list of elements by XPATH

  • get get a single element by CSS

  • getx get a single element by XPATH

These are very similar to how py finds elements. For example, the following code snippet will search the entire DOM (aka webpage) for the first Element with id=name and type "Carlos Kidman" into it.

py.get("#name").type("Carlos Kidman")

Now take a look at the next code snippet:

py.get(".form").get("#city").type("Salt Lake City")

This starts by searching the entire DOM for an element with class=form. Then, within the form element, search for the first element with id=city and type "Salt Lake City" into it.

Also, you can hold Element and Elements in variables instead of chaining them like the snippet above. The ability to set this context with a smaller scope can be powerful!

form = py.get(".form")
form.get("#name").type("Carlos Kidman")
form.get("#city").type("Salt Lake City")
form.get("#search").submit()

Last updated