mirror of
https://github.com/cypress-io/cypress.git
synced 2026-04-26 00:50:41 -05:00
1.9 KiB
1.9 KiB
title: closest comments: true
Get the first DOM element that matches the selector whether it be itself or one of it's ancestors.
| Returns | the new DOM element(s) found by the command. |
| Timeout | cy.filter will retry for the duration of the defaultCommandTimeout |
cy.closest( selector )
For each DOM element in the set, get the first DOM element that matches the selector by testing the DOM element itself and traversing up through its ancestors in the DOM tree.
Options
Pass in an options object to change the default behavior of cy.closest.
cy.closest( selector, options )
| Option | Default | Notes |
|---|---|---|
log |
true |
whether to display command in command log |
timeout |
defaultCommandTimeout |
Total time to retry getting the element |
Selector Usage
Find the closest element of the current subject with the class nav
cy.get("li.active").closest(".nav")
Command Log
Find the closest element of the current subject with the class nav
cy.get("li.active").closest(".nav")
The commands above will display in the command log as:
When clicking on the closest command within the command log, the console outputs the following: