> ## 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.

# click()

> Click an element you found, or click a point on the screen.

Click an element you've located with [`find()`](https://docs.testdriver.ai/find):

```javascript theme={null}
const signInButton = await testdriver.find('Sign in button');
await signInButton.click();
```

Or chain it straight onto `find()`:

```javascript theme={null}
await testdriver.find('Sign in button').click();
```

## element.click(action)

Clicks the element at the point where `find()` located it.

```typescript theme={null}
element.click(action?: ClickAction): Promise<void>
```

### Parameters

**action** `ClickAction` (default: click)

The mouse action to perform:

* `'click'`: a left click
* `'double-click'`: a double click, same as [`doubleClick()`](https://docs.testdriver.ai/double-click)
* `'right-click'`: a right click, same as [`rightClick()`](https://docs.testdriver.ai/right-click)
* `'hover'`: move the mouse there without clicking, same as [`hover()`](https://docs.testdriver.ai/hover)
* `'mouseDown'` and `'mouseUp'`: press or release the button, for dragging. See [mouseDown()](https://docs.testdriver.ai/mouse-down.md).

### Errors

Throws an `ElementNotFoundError` if `find()` didn't locate the element. Check `found()` first when the element might not be there:

```javascript theme={null}
const banner = await testdriver.find('cookie consent banner', { timeout: 0 });
if (banner.found()) {
  await testdriver.find('Accept all cookies button').click();
}
```

## testdriver.click(options)

Clicks a point on the screen by its coordinates. Prefer `element.click()`, which keeps working when the layout or screen size changes.

```typescript theme={null}
testdriver.click(options: { x: number; y: number; action?: ClickAction }): Promise<void>
```

### Parameters

**options** `object` *required*

**properties**

**x** `number` *required*

Distance from the left edge of the screen, in pixels.

**y** `number` *required*

Distance from the top edge of the screen, in pixels.

**action** `ClickAction` (default: click)

The mouse action, as for `element.click()`. `'hover'` doesn't move the mouse here; use [`testdriver.hover({ x, y })`](https://docs.testdriver.ai/hover) instead.

```javascript theme={null}
await testdriver.click({ x: 640, y: 360 });
await testdriver.click({ x: 640, y: 360, action: 'right-click' });
```

> [!NOTE]
>
> The older form `testdriver.click(x, y, action)` still works. Use the options object in new tests.

## Examples

### Open a context menu

```javascript theme={null}
await testdriver.find('report.pdf in the file list').click('right-click');
await testdriver.find('Rename in the context menu').click();
```

### Open a file

```javascript theme={null}
await testdriver.find('report.pdf in the file list').click('double-click');
```

### Drag an item

```javascript theme={null}
await testdriver.find('Task card titled "Write docs"').mouseDown();
await testdriver.find('Done column').hover();
await testdriver.find('Done column').mouseUp();
```

## Complete example

```javascript tests/login.test.mjs theme={null}
import { describe, expect, it } from 'vitest';
import { TestDriver } from 'testdriverai/vitest/hooks';

describe('Login', () => {
  it('shows an error for an empty form', async (context) => {
    const testdriver = TestDriver(context);
    await testdriver.provision.chrome({ url: 'https://testdriver-sandbox.vercel.app/login' });

    await testdriver.find('Sign in button below the password field').click();

    const result = await testdriver.assert('an error says the fields are required');
    expect(result).toBe(true);
  });
});
```

## Related

* [find()](https://docs.testdriver.ai/find.md): locate the element to click
* [doubleClick()](https://docs.testdriver.ai/double-click.md), [rightClick()](https://docs.testdriver.ai/right-click.md), and [hover()](https://docs.testdriver.ai/hover.md): shortcuts for the other actions
* [mouseDown()](https://docs.testdriver.ai/mouse-down.md) and [mouseUp()](https://docs.testdriver.ai/mouse-up.md): drag and drop
* [Perform actions](https://docs.testdriver.ai/performing-actions.md): every kind of action in one guide


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