---
title: "getByText(text[, options]) | Grafana k6 documentation"
description: "Browser module: page.getByText(text[, options]) method"
---

# getByText(text\[, options])

Returns a locator for elements containing the specified text. This method finds elements by their visible text content, making it ideal for locating user-facing content like buttons, links, headings, and other text elements.

Expand table

| Parameter       | Type             | Default | Description                                                                                                 |
|-----------------|------------------|---------|-------------------------------------------------------------------------------------------------------------|
| `text`          | string \| RegExp | \-      | Required. The text content to search for. Can be a string for exact match or a RegExp for pattern matching. |
| `options`       | object           | `null`  |                                                                                                             |
| `options.exact` | boolean          | `false` | Whether to match the text exactly with case sensitivity. When `true`, performs a case-sensitive match.      |

## Returns

Expand table

| Type                                                          | Description                                                                                    |
|---------------------------------------------------------------|------------------------------------------------------------------------------------------------|
| [Locator](/docs/k6/latest/javascript-api/k6-browser/locator/) | A locator object that can be used to interact with the elements containing the specified text. |

## Examples

Find and click elements by their visible text:

JavaScript ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```javascript
import { browser } from 'k6/browser';

export const options = {
  scenarios: {
    browser: {
      executor: 'shared-iterations',
      options: {
        browser: {
          type: 'chromium',
        },
      },
    },
  },
};

export default async function () {
  const page = await browser.newPage();

  try {
    await page.goto('https://quickpizza.grafana.com/');

    await page.getByText('Pizza, Please!').click();

    const noThanksBtn = page.getByText('No thanks');
    await noThanksBtn.click();
  } finally {
    await page.close();
  }
}
```

## Text matching behavior

**Whitespace normalization**: Text matching automatically normalizes whitespace, meaning:

- Multiple spaces become single spaces
- Line breaks become spaces
- Leading and trailing whitespace is ignored

Consider the following DOM structure:

HTML ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```html
<div>Hello <span>world</span></div>
<div>Hello</div>
```

You can locate by text substring, exact string, or a regular expression:

js ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```js
// Matches <span>
page.getByText('world');
// Matches first <div>
page.getByText('Hello world');
// Matches second <div>
page.getByText('Hello', { exact: true });
// Matches both <div>s
page.getByText(/Hello/);
// Matches second <div>
page.getByText(/^hello$/i);
```

## Common use cases

- **Button interactions**: Submit, Cancel, Delete, Edit buttons
- **Navigation**: Menu items, breadcrumbs, pagination links
- **Content verification**: Success messages, error messages, headings
- **Form interactions**: Checkbox labels, radio button options
- **Status indicators**: Active, Inactive, Pending states

## Best practices

1. **User-focused testing**: Using `getByText()` ensures your tests interact with content as users see it.
2. **Avoid brittle text**: Be cautious with exact text that might change frequently (like dates, counts, or user-generated content).
3. **Use meaningful text**: Prefer descriptive button text and labels over generic terms like “Click here” or “Button”.
4. **Handle dynamic content**: Use regular expressions for text that contains variable parts (timestamps, user names, counts).
5. **Consider accessibility**: Text-based selection encourages better accessibility practices in your application.
6. **Internationalization**: For multi-language applications, consider using test IDs or roles instead of text for critical interactions.

## Related

- [page.getByRole()](/docs/k6/latest/javascript-api/k6-browser/page/getbyrole/) - Locate by ARIA role
- [page.getByAltText()](/docs/k6/latest/javascript-api/k6-browser/page/getbyalttext/) - Locate by alt text
- [page.getByLabel()](/docs/k6/latest/javascript-api/k6-browser/page/getbylabel/) - Locate by form labels
- [page.getByPlaceholder()](/docs/k6/latest/javascript-api/k6-browser/page/getbyplaceholder/) - Locate by placeholder text
- [page.getByTestId()](/docs/k6/latest/javascript-api/k6-browser/page/getbytestid/) - Locate by test ID
- [page.getByTitle()](/docs/k6/latest/javascript-api/k6-browser/page/getbytitle/) - Locate by title attribute
