---
id: api/commands/first
title: cy.first()
description: Get the first DOM element within a set of DOM elements in Cypress.
section: api
source_path: docs/api/commands/first.mdx
version: 1934fa485ad0afcbbd3bdb674d5c5f1f991b55cf
updated_at: '2026-11-14T05:15:51.878Z'
---
# first

Get the first DOM element within a set of DOM elements.

The querying behavior of this command matches exactly how [`.first()`](https://api.jquery.com/first) works in jQuery.

## Syntax

```
.first()
.first(options)
```

### Usage

**Correct Usage**

```
cy.get('nav a').first() // Yield first link in nav
```

**Incorrect Usage**

```
cy.first() // Errors, cannot be chained off 'cy'
cy.getCookies().first() // Errors, 'getCookies' does not yield DOM element
```

### Arguments

**options _(Object)_**

Pass in an options object to change the default behavior of `.first()`.

| Option | Default | Description |
| --- | --- | --- |
| `log` | `true` | Displays the command in the [Command log](/llm/markdown/app/core-concepts/open-mode.md#Command-Log) |
| `timeout` | [`defaultCommandTimeout`](/llm/markdown/app/references/configuration.md#Timeouts) | Time to wait for `.first()` to resolve before [timing out](#Timeouts) |

### Yields

*   `.first()` yields the first DOM element within the set it was given.
*   `.first()` is a query, and it is _safe_ to chain further commands.

## Examples

### No Args

#### Get the first list item in a list

```
<ul>
  <li class="one">Knick knack on my thumb</li>
  <li class="two">Knick knack on my shoe</li>
  <li class="three">Knick knack on my knee</li>
  <li class="four">Knick knack on my door</li>
</ul>
```

```
// yields <li class="one">Knick knack on my thumb</li>
cy.get('li').first()
```

#### Get the first element across several lists

`.first()` yields one element for the whole set, even when the elements sit in different parents. The `:first-child` selector yields the first child of every parent instead.

```
<ul class="fruits">
  <li>Apple</li>
  <li>Banana</li>
</ul>
<ul class="vegetables">
  <li>Carrot</li>
  <li>Daikon</li>
</ul>
```

```
// yields <li>Apple</li>
cy.get('ul li').first()

// yields [<li>Apple</li>, <li>Carrot</li>]
cy.get('ul li:first-child')
```

### Common workflows with `.first()`

#### Scope `.within()` to the first matching element

[`.within()`](/llm/markdown/api/commands/within.md) throws an error when its subject contains more than one element. Narrow the set with `.first()` before calling it.

```
cy.get('tr')
  .first()
  .within(() => {
    cy.contains('Edit').click()
  })
```

#### Click the first result after the list finishes loading

An action such as [`.click()`](/llm/markdown/api/commands/click.md) is not an assertion, so `cy.get('[data-cy=result]').first().click()` acts on the first element that exists at that moment. If the list renders placeholder rows before the real results, the element it clicks can be a placeholder. Assert on the finished list before calling `.first()`, and Cypress retries the query until the assertion passes.

```
cy.get('[data-cy=result]').should('have.length', 10).first().click()
```

#### Assert on the first element while the list re-renders

`cy.get()` and `.first()` are both queries, so Cypress re-runs them together until the chained assertion passes. If the list re-renders and a new element takes the first position, the assertion checks the new element rather than the one that was first when the query started.

```
cy.get('[data-cy=todo]').first().should('have.text', 'Buy milk')
```

## Notes

### How `.first()` compares to `.eq(0)`

`.first()` yields the same element as [`.eq(0)`](/llm/markdown/api/commands/eq.md). Use `.eq()` to get an element at any other index, including negative indexes counted back from the end of the set.

### Selecting the first element that matches a selector

`.first()` takes no selector. `cy.get('li').first('.active')` yields the first `<li>` whether or not it has the class `active`. The Command Log still shows `.active` as the command's message, which can make it look as though the selector was applied. TypeScript flags the call as an error. To get the first element that matches a selector, narrow the set with [`.filter()`](/llm/markdown/api/commands/filter.md) before calling `.first()`.

```
cy.get('li').filter('.active').first()
```

## Rules

### Requirements

*   `.first()` requires being chained off a command that yields DOM element(s) or a document.

### Assertions

*   `.first()` will automatically [retry](/llm/markdown/app/core-concepts/retry-ability.md) until the element(s) [exist in the DOM](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Implicit-Assertions).
*   `.first()` will automatically [retry](/llm/markdown/app/core-concepts/retry-ability.md) until all chained assertions have passed.

### Timeouts

*   `.first()` can time out waiting for the element(s) to [exist in the DOM](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Implicit-Assertions).
*   `.first()` can time out waiting for assertions you've added to pass.

## Command Log

**_Find the first input in the form_**

```
cy.get('form').find('input').first()
```

The commands above display in the Command Log as:

Click the `first` command in the Command Log to print the following to the console:

## History

| Version | Changes |
| --- | --- |
| [12.6.0](/llm/markdown/app/references/changelog.md#12-6-0) | Overwrite `.first()` with [`Cypress.Commands.overwriteQuery()`](/llm/markdown/api/cypress-api/custom-queries.md#Overwriting-Existing-Queries) rather than `Cypress.Commands.overwrite()`. |
| [12.0.0](/llm/markdown/app/references/changelog.md#12-0-0) | `.first()` is now a query. |

## See also

*   [`.eq()`](/llm/markdown/api/commands/eq.md)
*   [`.filter()`](/llm/markdown/api/commands/filter.md)
*   [`.last()`](/llm/markdown/api/commands/last.md)
