|
| 1 | +--- |
| 2 | +title: 'getByPlaceholder(placeholder[, options])' |
| 3 | +description: 'Browser module: page.getByPlaceholder(placeholder[, options]) method' |
| 4 | +--- |
| 5 | + |
| 6 | +# getByPlaceholder(placeholder[, options]) |
| 7 | + |
| 8 | +Returns a locator for input elements with the specified `placeholder` attribute. This method is useful for locating form fields that use `placeholder` attribute to provide hints or examples to users about the expected input format. |
| 9 | + |
| 10 | +<TableWithNestedRows> |
| 11 | + |
| 12 | +| Parameter | Type | Default | Description | |
| 13 | +| ------------- | -------------- | ------- | ------------------------------------------------------------------------------------------------------------------ | |
| 14 | +| placeholder | string\|RegExp | - | Required. The placeholder 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 placeholder 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 input element(s) matching the specified placeholder text. | |
| 25 | + |
| 26 | +### Examples |
| 27 | + |
| 28 | +Find and fill inputs by their placeholder text: |
| 29 | + |
| 30 | +{{< code >}} |
| 31 | + |
| 32 | +```javascript |
| 33 | +import { browser } from 'k6/browser'; |
| 34 | + |
| 35 | +export const options = { |
| 36 | + scenarios: { |
| 37 | + browser: { |
| 38 | + executor: 'shared-iterations', |
| 39 | + options: { |
| 40 | + browser: { |
| 41 | + type: 'chromium', |
| 42 | + }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | +}; |
| 47 | + |
| 48 | +export default async function () { |
| 49 | + const page = await browser.newPage(); |
| 50 | + |
| 51 | + try { |
| 52 | + await page.setContent(` |
| 53 | + <input type="text" placeholder="First name"> |
| 54 | + <input type="text" placeholder="Last name"> |
| 55 | + <input type="text" placeholder="dd/mm/yyyy"> |
| 56 | + <input type="text" placeholder="[email protected]"> |
| 57 | + <input type="text" placeholder="+1 (555) 123-4567"> |
| 58 | + `); |
| 59 | + |
| 60 | + await page.getByPlaceholder('First name').fill('First'); |
| 61 | + await page.getByPlaceholder('Last name').fill('Last'); |
| 62 | + await page.getByPlaceholder('dd/mm/yyyy').fill('01/01/1990'); |
| 63 | + |
| 64 | + await page. getByPlaceholder( '[email protected]'). fill( '[email protected]'); |
| 65 | + await page.getByPlaceholder('+1 (555) 123-4567').fill('+1 (555) 987-6543'); |
| 66 | + } finally { |
| 67 | + await page.close(); |
| 68 | + } |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +{{< /code >}} |
| 73 | + |
| 74 | +### Common use cases |
| 75 | + |
| 76 | +**Form field identification:** |
| 77 | + |
| 78 | +- Login and registration forms without explicit labels |
| 79 | +- Quick search boxes |
| 80 | +- Filter and input controls |
| 81 | +- Comment and feedback forms |
| 82 | + |
| 83 | +**E-commerce:** |
| 84 | + |
| 85 | +- Product search bars |
| 86 | +- Quantity input fields |
| 87 | +- Promo code entry |
| 88 | +- Address and payment information |
| 89 | + |
| 90 | +**Interactive applications:** |
| 91 | + |
| 92 | +- Chat input fields |
| 93 | +- Command input interfaces |
| 94 | +- Settings and configuration forms |
| 95 | +- Data entry applications |
| 96 | + |
| 97 | +### Best practices |
| 98 | + |
| 99 | +1. **Complement, don't replace labels**: Placeholder text should supplement, not replace proper form labels for accessibility. |
| 100 | + |
| 101 | +2. **Use descriptive placeholders**: Ensure placeholder text clearly indicates the expected input format or content. |
| 102 | + |
| 103 | +3. **Consider internationalization**: When testing multi-language applications, be aware that placeholder text may change. |
| 104 | + |
| 105 | +4. **Accessibility considerations**: Remember that placeholder text alone may not be sufficient for users with disabilities. |
| 106 | + |
| 107 | +### Related |
| 108 | + |
| 109 | +- [page.getByRole()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbyrole/) - Locate by ARIA role |
| 110 | +- [page.getByAltText()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbyalttext/) - Locate by alt text |
| 111 | +- [page.getByLabel()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbylabel/) - Locate by form labels (preferred for accessibility) |
| 112 | +- [page.getByTestId()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbytestid/) - Locate by test ID |
| 113 | +- [page.getByTitle()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbytitle/) - Locate by title attribute |
| 114 | +- [page.getByText()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/page/getbytext/) - Locate by visible text |
0 commit comments