{
  "doc": {
    "id": "api/commands/focus",
    "title": "cy.focus()",
    "description": "Focus on a DOM element in Cypress.",
    "section": "api",
    "source_path": "/llm/markdown/api/commands/focus.md",
    "version": "c4ad6f88c531de58221c73dd91a75af7138334b8",
    "updated_at": "2026-11-10T01:15:02.829Z",
    "headings": [
      {
        "id": "api/commands/focus#focus",
        "text": "focus",
        "level": 1
      },
      {
        "id": "api/commands/focus#syntax",
        "text": "Syntax",
        "level": 2
      },
      {
        "id": "api/commands/focus#usage",
        "text": "Usage",
        "level": 3
      },
      {
        "id": "api/commands/focus#arguments",
        "text": "Arguments",
        "level": 3
      },
      {
        "id": "api/commands/focus#yields",
        "text": "Yields",
        "level": 3
      },
      {
        "id": "api/commands/focus#examples",
        "text": "Examples",
        "level": 2
      },
      {
        "id": "api/commands/focus#no-args",
        "text": "No Args",
        "level": 3
      },
      {
        "id": "api/commands/focus#focus-an-input-and-confirm-it-has-focus",
        "text": "Focus an input and confirm it has focus",
        "level": 4
      },
      {
        "id": "api/commands/focus#focus-one-element-from-several-matches",
        "text": "Focus one element from several matches",
        "level": 4
      },
      {
        "id": "api/commands/focus#focus-an-element-made-focusable-with-tabindex",
        "text": "Focus an element made focusable with tabindex",
        "level": 4
      },
      {
        "id": "api/commands/focus#focus-workflows-for-forms-and-keyboard-testing",
        "text": "Focus workflows for forms and keyboard testing",
        "level": 3
      },
      {
        "id": "api/commands/focus#validate-a-field-when-it-loses-focus",
        "text": "Validate a field when it loses focus",
        "level": 4
      },
      {
        "id": "api/commands/focus#open-ui-that-appears-on-focus",
        "text": "Open UI that appears on focus",
        "level": 4
      },
      {
        "id": "api/commands/focus#test-keyboard-tab-order",
        "text": "Test keyboard tab order",
        "level": 4
      },
      {
        "id": "api/commands/focus#notes",
        "text": "Notes",
        "level": 2
      },
      {
        "id": "api/commands/focus#actionability",
        "text": "Actionability",
        "level": 3
      },
      {
        "id": "api/commands/focus#focus-is-not-an-action-command",
        "text": "Focus is not an action command",
        "level": 4
      },
      {
        "id": "api/commands/focus#blur-events",
        "text": "Blur Events",
        "level": 3
      },
      {
        "id": "api/commands/focus#cypress-blurs-other-focused-elements-first",
        "text": "Cypress blurs other focused elements first",
        "level": 4
      },
      {
        "id": "api/commands/focus#focusable",
        "text": "Focusable",
        "level": 3
      },
      {
        "id": "api/commands/focus#can-only-be-called-on-a-valid-focusable-element",
        "text": "Can only be called on a valid focusable element",
        "level": 4
      },
      {
        "id": "api/commands/focus#rules",
        "text": "Rules",
        "level": 2
      },
      {
        "id": "api/commands/focus#requirements",
        "text": "Requirements",
        "level": 3
      },
      {
        "id": "api/commands/focus#assertions",
        "text": "Assertions",
        "level": 3
      },
      {
        "id": "api/commands/focus#timeouts",
        "text": "Timeouts",
        "level": 3
      },
      {
        "id": "api/commands/focus#command-log",
        "text": "Command Log",
        "level": 2
      },
      {
        "id": "api/commands/focus#see-also",
        "text": "See also",
        "level": 2
      }
    ]
  },
  "chunks": [
    {
      "id": "api/commands/focus#syntax",
      "doc_id": "api/commands/focus",
      "heading": "Syntax",
      "heading_level": 2,
      "content_markdown": "## Syntax\n\n```\n.focus()\n.focus(options)\n```\n\n### Usage\n\n**Correct Usage**\n\n```\ncy.get('input').first().focus() // Focus on the first input\ncy.window().focus() // Focus on the window\n```\n\n**Incorrect Usage**\n\n```\ncy.focus('#search') // Errors, cannot be chained off 'cy'\ncy.get('div:first').focus() // Errors, a plain div is not focusable\n```\n\n### Arguments\n\n**options _(Object)_**\n\nPass in an options object to change the default behavior of `.focus()`.\n\n| Option | Default | Description |\n| --- | --- | --- |\n| `log` | `true` | Displays the command in the [Command Log](/llm/markdown/app/core-concepts/open-mode.md#Command-Log) |\n| `timeout` | [`defaultCommandTimeout`](/llm/markdown/app/references/configuration.md#Timeouts) | Time to wait for assertions chained to `.focus()` to pass before [timing out](#Timeouts) |\n\n### Yields\n\n*   `.focus()` yields the same subject it was given.\n*   It is [unsafe](/llm/markdown/app/core-concepts/retry-ability.md#Only-queries-are-retried) to chain further commands that rely on the subject after `.focus()`.\n",
      "section": "api",
      "anchors": [
        "syntax"
      ],
      "path": "/llm/json/chunked/api/commands/focus.json",
      "token_estimate": 179
    },
    {
      "id": "api/commands/focus#usage",
      "doc_id": "api/commands/focus",
      "heading": "Usage",
      "heading_level": 3,
      "content_markdown": "### Usage\n\n**Correct Usage**\n\n```\ncy.get('input').first().focus() // Focus on the first input\ncy.window().focus() // Focus on the window\n```\n\n**Incorrect Usage**\n\n```\ncy.focus('#search') // Errors, cannot be chained off 'cy'\ncy.get('div:first').focus() // Errors, a plain div is not focusable\n```\n",
      "section": "api",
      "anchors": [
        "usage"
      ],
      "path": "/llm/json/chunked/api/commands/focus.json",
      "token_estimate": 53
    },
    {
      "id": "api/commands/focus#arguments",
      "doc_id": "api/commands/focus",
      "heading": "Arguments",
      "heading_level": 3,
      "content_markdown": "### Arguments\n\n**options _(Object)_**\n\nPass in an options object to change the default behavior of `.focus()`.\n\n| Option | Default | Description |\n| --- | --- | --- |\n| `log` | `true` | Displays the command in the [Command Log](/llm/markdown/app/core-concepts/open-mode.md#Command-Log) |\n| `timeout` | [`defaultCommandTimeout`](/llm/markdown/app/references/configuration.md#Timeouts) | Time to wait for assertions chained to `.focus()` to pass before [timing out](#Timeouts) |\n",
      "section": "api",
      "anchors": [
        "arguments"
      ],
      "path": "/llm/json/chunked/api/commands/focus.json",
      "token_estimate": 83
    },
    {
      "id": "api/commands/focus#examples",
      "doc_id": "api/commands/focus",
      "heading": "Examples",
      "heading_level": 2,
      "content_markdown": "## Examples\n\n### No Args\n\n#### Focus an input and confirm it has focus\n\n```\ncy.get('input[name=\"email\"]').focus().should('have.focus')\n```\n\nTo check which element has focus without a reference to it, use [`cy.focused()`](/llm/markdown/api/commands/focused.md).\n\n#### Focus one element from several matches\n\n`.focus()` works on a single element, so narrow the subject first when your selector matches more than one:\n\n```\ncy.get('input').first().focus()\ncy.get('button').eq(2).focus()\n```\n\n#### Focus an element made focusable with `tabindex`\n\nCustom widgets such as dialogs and menus often use `tabindex` to become focusable:\n\n```\ncy.get('[role=\"dialog\"][tabindex=\"-1\"]').focus()\n```\n\n### Focus workflows for forms and keyboard testing\n\n#### Validate a field when it loses focus\n\n```\ncy.get('input[name=\"email\"]').focus()\ncy.get('input[name=\"email\"]').blur()\ncy.get('.error').should('contain', 'Email is required')\n```\n\nYou don't need `.focus()` before [`.type()`](/llm/markdown/api/commands/type.md), since `.type()` focuses the element first. Reach for `.focus()` when you need focus without typing.\n\n#### Open UI that appears on focus\n\n```\ncy.get('input[name=\"search\"]').focus()\ncy.get('[role=\"listbox\"]').should('be.visible')\n```\n\n#### Test keyboard tab order\n\nSet the starting point with `.focus()`, move focus with [`cy.press()`](/llm/markdown/api/commands/press.md), and check where it went with [`cy.focused()`](/llm/markdown/api/commands/focused.md):\n\n```\ncy.get('input[name=\"first-name\"]').focus()\ncy.press(Cypress.Keyboard.Keys.TAB)\ncy.focused().should('have.attr', 'name', 'last-name')\n```\n",
      "section": "api",
      "anchors": [
        "examples"
      ],
      "path": "/llm/json/chunked/api/commands/focus.json",
      "token_estimate": 224
    },
    {
      "id": "api/commands/focus#no-args",
      "doc_id": "api/commands/focus",
      "heading": "No Args",
      "heading_level": 3,
      "content_markdown": "### No Args\n\n#### Focus an input and confirm it has focus\n\n```\ncy.get('input[name=\"email\"]').focus().should('have.focus')\n```\n\nTo check which element has focus without a reference to it, use [`cy.focused()`](/llm/markdown/api/commands/focused.md).\n\n#### Focus one element from several matches\n\n`.focus()` works on a single element, so narrow the subject first when your selector matches more than one:\n\n```\ncy.get('input').first().focus()\ncy.get('button').eq(2).focus()\n```\n\n#### Focus an element made focusable with `tabindex`\n\nCustom widgets such as dialogs and menus often use `tabindex` to become focusable:\n\n```\ncy.get('[role=\"dialog\"][tabindex=\"-1\"]').focus()\n```\n",
      "section": "api",
      "anchors": [
        "no-args"
      ],
      "path": "/llm/json/chunked/api/commands/focus.json",
      "token_estimate": 108
    },
    {
      "id": "api/commands/focus#focus-workflows-for-forms-and-keyboard-testing",
      "doc_id": "api/commands/focus",
      "heading": "Focus workflows for forms and keyboard testing",
      "heading_level": 3,
      "content_markdown": "### Focus workflows for forms and keyboard testing\n\n#### Validate a field when it loses focus\n\n```\ncy.get('input[name=\"email\"]').focus()\ncy.get('input[name=\"email\"]').blur()\ncy.get('.error').should('contain', 'Email is required')\n```\n\nYou don't need `.focus()` before [`.type()`](/llm/markdown/api/commands/type.md), since `.type()` focuses the element first. Reach for `.focus()` when you need focus without typing.\n\n#### Open UI that appears on focus\n\n```\ncy.get('input[name=\"search\"]').focus()\ncy.get('[role=\"listbox\"]').should('be.visible')\n```\n\n#### Test keyboard tab order\n\nSet the starting point with `.focus()`, move focus with [`cy.press()`](/llm/markdown/api/commands/press.md), and check where it went with [`cy.focused()`](/llm/markdown/api/commands/focused.md):\n\n```\ncy.get('input[name=\"first-name\"]').focus()\ncy.press(Cypress.Keyboard.Keys.TAB)\ncy.focused().should('have.attr', 'name', 'last-name')\n```\n",
      "section": "api",
      "anchors": [
        "focus-workflows-for-forms-and-keyboard-testing"
      ],
      "path": "/llm/json/chunked/api/commands/focus.json",
      "token_estimate": 113
    },
    {
      "id": "api/commands/focus#validate-a-field-when-it-loses-focus",
      "doc_id": "api/commands/focus",
      "heading": "Validate a field when it loses focus",
      "heading_level": 4,
      "content_markdown": "#### Validate a field when it loses focus\n\n```\ncy.get('input[name=\"email\"]').focus()\ncy.get('input[name=\"email\"]').blur()\ncy.get('.error').should('contain', 'Email is required')\n```\n\nYou don't need `.focus()` before [`.type()`](/llm/markdown/api/commands/type.md), since `.type()` focuses the element first. Reach for `.focus()` when you need focus without typing.\n",
      "section": "api",
      "anchors": [
        "validate-a-field-when-it-loses-focus"
      ],
      "path": "/llm/json/chunked/api/commands/focus.json",
      "token_estimate": 49
    },
    {
      "id": "api/commands/focus#notes",
      "doc_id": "api/commands/focus",
      "heading": "Notes",
      "heading_level": 2,
      "content_markdown": "## Notes\n\n### Actionability\n\n#### Focus is not an action command\n\n`.focus()` is not implemented like other action commands, and does not follow the same rules of [waiting for actionability](/llm/markdown/app/core-concepts/interacting-with-elements.md).\n\n`.focus()` is a helpful command used as a shortcut. Normally there's no way for a user to \"focus\" an element without causing another action or side effect. Typically the user would have to click or tab to this element.\n\nOftentimes using `.focus()` directly is more concise and conveys what you're trying to test.\n\nIf you want the other guarantees of waiting for an element to become actionable, you should use a different command like [`.click()`](/llm/markdown/api/commands/click.md).\n\n### Blur Events\n\n#### Cypress blurs other focused elements first\n\nIf there is currently a different DOM element with focus, Cypress issues a `blur` event to that element before running the `.focus()` command.\n\n### Focusable\n\n#### Can only be called on a valid focusable element\n\nCypress treats these elements as focusable:\n\n*   `a` and `area` elements with an `href`\n*   `input`, `select`, `textarea`, and `button` elements that aren't `disabled`\n*   `iframe` elements\n*   Any element with a `tabindex` or `contenteditable` attribute\n\nCalling `.focus()` on anything else fails with an error. The window and the `body` are the two exceptions:\n\n*   `cy.window().focus()` fires a `focus` event on the window. Focus itself does not move: whichever element held it still does, and `document.activeElement` is unchanged.\n*   `cy.get('body').focus()` passes without error but fires no events, since the `body` doesn't receive focus. The exception is a `contenteditable` body, which Cypress focuses like any other element.\n",
      "section": "api",
      "anchors": [
        "notes"
      ],
      "path": "/llm/json/chunked/api/commands/focus.json",
      "token_estimate": 341
    },
    {
      "id": "api/commands/focus#actionability",
      "doc_id": "api/commands/focus",
      "heading": "Actionability",
      "heading_level": 3,
      "content_markdown": "### Actionability\n\n#### Focus is not an action command\n\n`.focus()` is not implemented like other action commands, and does not follow the same rules of [waiting for actionability](/llm/markdown/app/core-concepts/interacting-with-elements.md).\n\n`.focus()` is a helpful command used as a shortcut. Normally there's no way for a user to \"focus\" an element without causing another action or side effect. Typically the user would have to click or tab to this element.\n\nOftentimes using `.focus()` directly is more concise and conveys what you're trying to test.\n\nIf you want the other guarantees of waiting for an element to become actionable, you should use a different command like [`.click()`](/llm/markdown/api/commands/click.md).\n",
      "section": "api",
      "anchors": [
        "actionability"
      ],
      "path": "/llm/json/chunked/api/commands/focus.json",
      "token_estimate": 137
    },
    {
      "id": "api/commands/focus#focus-is-not-an-action-command",
      "doc_id": "api/commands/focus",
      "heading": "Focus is not an action command",
      "heading_level": 4,
      "content_markdown": "#### Focus is not an action command\n\n`.focus()` is not implemented like other action commands, and does not follow the same rules of [waiting for actionability](/llm/markdown/app/core-concepts/interacting-with-elements.md).\n\n`.focus()` is a helpful command used as a shortcut. Normally there's no way for a user to \"focus\" an element without causing another action or side effect. Typically the user would have to click or tab to this element.\n\nOftentimes using `.focus()` directly is more concise and conveys what you're trying to test.\n\nIf you want the other guarantees of waiting for an element to become actionable, you should use a different command like [`.click()`](/llm/markdown/api/commands/click.md).\n",
      "section": "api",
      "anchors": [
        "focus-is-not-an-action-command"
      ],
      "path": "/llm/json/chunked/api/commands/focus.json",
      "token_estimate": 135
    },
    {
      "id": "api/commands/focus#blur-events",
      "doc_id": "api/commands/focus",
      "heading": "Blur Events",
      "heading_level": 3,
      "content_markdown": "### Blur Events\n\n#### Cypress blurs other focused elements first\n\nIf there is currently a different DOM element with focus, Cypress issues a `blur` event to that element before running the `.focus()` command.\n",
      "section": "api",
      "anchors": [
        "blur-events"
      ],
      "path": "/llm/json/chunked/api/commands/focus.json",
      "token_estimate": 44
    },
    {
      "id": "api/commands/focus#cypress-blurs-other-focused-elements-first",
      "doc_id": "api/commands/focus",
      "heading": "Cypress blurs other focused elements first",
      "heading_level": 4,
      "content_markdown": "#### Cypress blurs other focused elements first\n\nIf there is currently a different DOM element with focus, Cypress issues a `blur` event to that element before running the `.focus()` command.\n",
      "section": "api",
      "anchors": [
        "cypress-blurs-other-focused-elements-first"
      ],
      "path": "/llm/json/chunked/api/commands/focus.json",
      "token_estimate": 40
    },
    {
      "id": "api/commands/focus#focusable",
      "doc_id": "api/commands/focus",
      "heading": "Focusable",
      "heading_level": 3,
      "content_markdown": "### Focusable\n\n#### Can only be called on a valid focusable element\n\nCypress treats these elements as focusable:\n\n*   `a` and `area` elements with an `href`\n*   `input`, `select`, `textarea`, and `button` elements that aren't `disabled`\n*   `iframe` elements\n*   Any element with a `tabindex` or `contenteditable` attribute\n\nCalling `.focus()` on anything else fails with an error. The window and the `body` are the two exceptions:\n\n*   `cy.window().focus()` fires a `focus` event on the window. Focus itself does not move: whichever element held it still does, and `document.activeElement` is unchanged.\n*   `cy.get('body').focus()` passes without error but fires no events, since the `body` doesn't receive focus. The exception is a `contenteditable` body, which Cypress focuses like any other element.\n",
      "section": "api",
      "anchors": [
        "focusable"
      ],
      "path": "/llm/json/chunked/api/commands/focus.json",
      "token_estimate": 157
    },
    {
      "id": "api/commands/focus#can-only-be-called-on-a-valid-focusable-element",
      "doc_id": "api/commands/focus",
      "heading": "Can only be called on a valid focusable element",
      "heading_level": 4,
      "content_markdown": "#### Can only be called on a valid focusable element\n\nCypress treats these elements as focusable:\n\n*   `a` and `area` elements with an `href`\n*   `input`, `select`, `textarea`, and `button` elements that aren't `disabled`\n*   `iframe` elements\n*   Any element with a `tabindex` or `contenteditable` attribute\n\nCalling `.focus()` on anything else fails with an error. The window and the `body` are the two exceptions:\n\n*   `cy.window().focus()` fires a `focus` event on the window. Focus itself does not move: whichever element held it still does, and `document.activeElement` is unchanged.\n*   `cy.get('body').focus()` passes without error but fires no events, since the `body` doesn't receive focus. The exception is a `contenteditable` body, which Cypress focuses like any other element.\n",
      "section": "api",
      "anchors": [
        "can-only-be-called-on-a-valid-focusable-element"
      ],
      "path": "/llm/json/chunked/api/commands/focus.json",
      "token_estimate": 155
    },
    {
      "id": "api/commands/focus#rules",
      "doc_id": "api/commands/focus",
      "heading": "Rules",
      "heading_level": 2,
      "content_markdown": "## Rules\n\n### Requirements\n\n*   `.focus()` requires being chained off a command that yields a DOM element or the window.\n*   `.focus()` requires the subject to contain a single element.\n*   `.focus()` requires the element to be able to receive focus. The window and the `body` are exempt.\n\n### Assertions\n\n*   `.focus()` will automatically wait for assertions you have chained to pass.\n\n### Timeouts\n\n*   `.focus()` can time out waiting for assertions you've added to pass.\n",
      "section": "api",
      "anchors": [
        "rules"
      ],
      "path": "/llm/json/chunked/api/commands/focus.json",
      "token_estimate": 101
    },
    {
      "id": "api/commands/focus#requirements",
      "doc_id": "api/commands/focus",
      "heading": "Requirements",
      "heading_level": 3,
      "content_markdown": "### Requirements\n\n*   `.focus()` requires being chained off a command that yields a DOM element or the window.\n*   `.focus()` requires the subject to contain a single element.\n*   `.focus()` requires the element to be able to receive focus. The window and the `body` are exempt.\n",
      "section": "api",
      "anchors": [
        "requirements"
      ],
      "path": "/llm/json/chunked/api/commands/focus.json",
      "token_estimate": 61
    },
    {
      "id": "api/commands/focus#command-log",
      "doc_id": "api/commands/focus",
      "heading": "Command Log",
      "heading_level": 2,
      "content_markdown": "## Command Log\n\n**_Focus the textarea_**\n\n```\ncy.get('[name=\"comment\"]').focus()\n```\n\nThe commands above will display in the Command Log as:\n\nWhen clicking on the `focus` command within the command log, the console outputs the following:\n",
      "section": "api",
      "anchors": [
        "command-log"
      ],
      "path": "/llm/json/chunked/api/commands/focus.json",
      "token_estimate": 45
    }
  ]
}