Documentation for automated readers
A curated documentation index is available at: https://grafana.com/llms.txt
A complete documentation index is available at: https://grafana.com/llms-full.txt
These indexes can help with page discovery before fetching individual documents.
This page is also available in Markdown, which may be easier for automated readers and AI tools to parse than HTML. The Markdown version is available at https://grafana.com/docs/k6/next/javascript-api/k6-browser/frame/getbytext.md, or by sending Accept: text/markdown to https://grafana.com/docs/k6/next/javascript-api/k6-browser/frame/getbytext/. For broader documentation discovery, the curated index is available at https://grafana.com/llms.txt and the complete index is available at https://grafana.com/llms-full.txt.
This is documentation for the next version of Grafana k6 documentation. For the latest stable release, go to the latest version.
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.
| 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
| Type | Description |
|---|---|
| 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:
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/');
const frame = page.mainFrame();
await frame.getByText('Pizza, Please!').click();
const noThanksBtn = frame.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:
<div>Hello <span>world</span></div>
<div>Hello</div>You can locate by text substring, exact string, or a regular expression:
// 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
- User-focused testing: Using
getByText()ensures your tests interact with content as users see it. - Avoid brittle text: Be cautious with exact text that might change frequently (like dates, counts, or user-generated content).
- Use meaningful text: Prefer descriptive button text and labels over generic terms like “Click here” or “Button”.
- Handle dynamic content: Use regular expressions for text that contains variable parts (timestamps, user names, counts).
- Consider accessibility: Text-based selection encourages better accessibility practices in your application.
- Internationalization: For multi-language applications, consider using test IDs or roles instead of text for critical interactions.
Related
- frame.getByRole() - Locate by ARIA role
- frame.getByAltText() - Locate by alt text
- frame.getByLabel() - Locate by form labels
- frame.getByPlaceholder() - Locate by placeholder text
- frame.getByTestId() - Locate by test ID
- frame.getByTitle() - Locate by title attribute
Was this page helpful?
Related resources from Grafana Labs

