> ## Documentation Index
> Fetch the complete documentation index at: https://docs.testdriver.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Handle an element that isn't found

> See how find() returns an unfound element instead of throwing when nothing matches.

This test looks for an element that isn't on the page. Use this pattern when an element may not appear.

## Watch it run

[https://api-test.testdriver.ai/api/v1/testdriver/testcase/6af50628b2a40e1e66d152ff/replay](https://api-test.testdriver.ai/api/v1/testdriver/testcase/6af50628b2a40e1e66d152ff/replay)

## The test

```javascript title="element-not-found.test.mjs" theme={null}
import { describe, expect, it } from "vitest";
import { TestDriver } from "testdriverai/vitest/hooks";

describe("Element Not Found Test", () => {
  it("should handle non-existent element gracefully without timing out", async (context) => {
    const testdriver = TestDriver(context);
    await testdriver.provision.chrome({ url: 'http://testdriver-sandbox.vercel.app/login' });

    // Try to find an element that definitely doesn't exist
    const element = await testdriver.find(
      "a purple unicorn dancing on the moon",
    );

    // Should return an element that is not found
    expect(element.found()).toBe(false);
    expect(element.coordinates).toBeNull();
  });
});
```

## How it works

* By default, `find()` checks the screen every 5 seconds for up to 10 seconds.
* When nothing matches, `find()` doesn't throw.
* Instead, it returns an element whose `found()` is `false` and whose `coordinates` are `null`.
* Calling `click()` on an unfound element throws an `ElementNotFoundError`.

## Run it

The test is in the [TestDriver SDK repository](https://github.com/testdriverai/testdriverai/tree/main/examples). Clone it, install dependencies, and run the file:

```bash theme={null}
git clone https://github.com/testdriverai/testdriverai
cd testdriverai
npm install
npx vitest run examples/element-not-found.test.mjs
```

The test reads your API key from `TD_API_KEY`. Create a key at [console.testdriver.ai/team/api-keys](https://console.testdriver.ai/team/api-keys). Then set it in your environment or a `.env` file.

## Related

* [find()](https://docs.testdriver.ai/find.md)
* [Locate elements](https://docs.testdriver.ai/locating-elements.md)
* [Errors](https://docs.testdriver.ai/errors.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.