|
| 1 | +--- |
| 2 | +title: 'getByTitle(title[, options])' |
| 3 | +description: 'Browser module: page.getByTitle(title[, options]) method' |
| 4 | +--- |
| 5 | + |
| 6 | +# getByTitle(title[, options]) |
| 7 | + |
| 8 | +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. |
| 9 | + |
| 10 | +<TableWithNestedRows> |
| 11 | + |
| 12 | +| Parameter | Type | Default | Description | |
| 13 | +| ------------- | -------------- | ------- | ------------------------------------------------------------------------------------------------------------ | |
| 14 | +| title | string\|RegExp | - | Required. The title text to search for. Can be a string for exact match or a RegExp for pattern matching. | |
| 15 | +| options | object | `null` | | |
| 16 | +| options.exact | boolean | `false` | Whether to match the title text exactly with case sensitivity. When `true`, performs a case-sensitive match. | |
| 17 | + |
| 18 | +</TableWithNestedRows> |
| 19 | + |
| 20 | +### Returns |
| 21 | + |
| 22 | +| Type | Description | |
| 23 | +| -------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------- | |
| 24 | +| [Locator](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/) | A locator object that can be used to interact with the element(s) matching the specified title attribute. | |
| 25 | + |
| 26 | +### Examples |
| 27 | + |
| 28 | +Find and interact with elements by their title attribute: |
| 29 | + |
| 30 | +{{< code >}} |
| 31 | + |
| 32 | +```javascript |
| 33 | +import { browser } from 'k6/browser'; |
| 34 | +import { expect } from 'https://jslib.k6.io/k6-testing/0.5.0/index.js'; |
| 35 | + |
| 36 | +export const options = { |
| 37 | + scenarios: { |
| 38 | + browser: { |
| 39 | + executor: 'shared-iterations', |
| 40 | + options: { |
| 41 | + browser: { |
| 42 | + type: 'chromium', |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | + }, |
| 47 | +}; |
| 48 | + |
| 49 | +export default async function () { |
| 50 | + const page = await browser.newPage(); |
| 51 | + |
| 52 | + try { |
| 53 | + await page.setContent(` |
| 54 | + <button title="Hello World">Hi</button> |
| 55 | + <select title="Favorite Color"> |
| 56 | + <option value="Red">Red</option> |
| 57 | + <option value="Green">Green</option> |
| 58 | + <option value="Blue">Blue</option> |
| 59 | + </select> |
| 60 | + <input type="checkbox" title="Check me"> |
| 61 | + `); |
| 62 | + |
| 63 | + await page.getByTitle('Hello World').click(); |
| 64 | + |
| 65 | + await page.getByTitle('Favorite Color').selectOption('Red'); |
| 66 | + |
| 67 | + await page.getByTitle('Check me').check(); |
| 68 | + } finally { |
| 69 | + await page.close(); |
| 70 | + } |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +{{< /code >}} |
| 75 | + |
| 76 | +### Common use cases |
| 77 | + |
| 78 | +**User interface controls:** |
| 79 | + |
| 80 | +- Toolbar buttons and action items |
| 81 | +- Navigation controls (previous/next, pagination) |
| 82 | +- Media player controls |
| 83 | +- Menu and dropdown triggers |
| 84 | + |
| 85 | +**Informational elements:** |
| 86 | + |
| 87 | +- Help icons and tooltips |
| 88 | +- Status indicators and badges |
| 89 | +- Progress indicators |
| 90 | +- Warning and error messages |
| 91 | + |
| 92 | +**Accessibility support:** |
| 93 | + |
| 94 | +- Screen reader descriptions |
| 95 | +- Alternative text for complex elements |
| 96 | +- Context-sensitive help |
| 97 | +- Form field explanations |
| 98 | + |
| 99 | +### Best practices |
| 100 | + |
| 101 | +1. **Meaningful titles**: Ensure title attributes provide clear, helpful information about the element's purpose or content. |
| 102 | + |
| 103 | +2. **Accessibility compliance**: Use titles to enhance accessibility, especially for elements that might not have clear visual labels. |
| 104 | + |
| 105 | +3. **Avoid redundancy**: Don't duplicate visible text in the title attribute unless providing additional context. |
| 106 | + |
| 107 | +4. **Dynamic content**: When testing applications with changing title content, use flexible matching patterns or regular expressions. |
| 108 | + |
| 109 | +5. **Tooltip testing**: Remember that title attributes often create tooltips on hover, which can be useful for UI testing. |
| 110 | + |
| 111 | +6. **Internationalization**: Consider that title text may change in different language versions of your application. |
| 112 | + |
| 113 | +### Related |
| 114 | + |
| 115 | +- [page.getByRole()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbyrole/) - Locate by ARIA role |
| 116 | +- [page.getByAltText()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbyalttext/) - Locate by alt text |
| 117 | +- [page.getByLabel()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbylabel/) - Locate by form labels |
| 118 | +- [page.getByPlaceholder()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbyplaceholder/) - Locate by placeholder text |
| 119 | +- [page.getByTestId()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbytestid/) - Locate by test ID |
| 120 | +- [page.getByText()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbytext/) - Locate by visible text |
0 commit comments