Menu
Choose a product
Viewing: v1.7.x (latest)
Find another version
Scroll for more
Open source
getByTitle(title[, options])
Returns a locator for elements with the specified title attribute. This method is useful for locating elements that use the title attribute to provide additional information, tooltips, or accessibility context.
| Parameter | Type | Default | Description |
|---|---|---|---|
title | string | RegExp | - | Required. The title text 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 title 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 matching the specified title attribute. |
Examples
Find and interact with elements by their title attribute:
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.setContent(`
<button title="Hello World">Hi</button>
<select title="Favorite Color">
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
</select>
<input type="checkbox" title="Check me">
`);
const frame = page.mainFrame();
await frame.getByTitle('Hello World').click();
await frame.getByTitle('Favorite Color').selectOption('Red');
await frame.getByTitle('Check me').check();
} finally {
await page.close();
}
}Common use cases
- User interface controls:
- Toolbar buttons and action items
- Navigation controls (previous/next, pagination)
- Media player controls
- Menu and drop-down triggers
- Informational elements:
- Help icons and tooltips
- Status indicators and badges
- Progress indicators
- Warning and error messages
- Accessibility support:
- Screen reader descriptions
- Alternative text for complex elements
- Context-sensitive help
- Form field explanations
Best practices
- Meaningful titles: Ensure title attributes provide clear, helpful information about the element’s purpose or content.
- Accessibility compliance: Use titles to enhance accessibility, especially for elements that might not have clear visual labels.
- Avoid redundancy: Don’t duplicate visible text in the title attribute unless providing additional context.
- Dynamic content: When testing applications with changing title content, use flexible matching patterns or regular expressions.
- Tooltip testing: Remember that title attributes often create tooltips on hover, which can be useful for UI testing.
- Internationalization: Consider that title text may change in different language versions of your application.
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.getByText() - Locate by visible text
Was this page helpful?
Related resources from Grafana Labs
Additional helpful documentation, links, and articles:
Video

Performance testing and observability in Grafana Cloud
Optimize user experiences with Grafana Cloud. Learn real-time insights, performance testing with k6, and continuous validation with Synthetic Monitoring.
Events

User-centered observability: load testing, real user monitoring, and synthetics
Learn how to use load testing, synthetic monitoring, and real user monitoring (RUM) to understand end users' experience of your apps. Watch on demand.