|
| 1 | +--- |
| 2 | +title: 'locator(selector[, options])' |
| 3 | +description: 'Browser module: frameLocator.locator(selector[, options]) method' |
| 4 | +--- |
| 5 | + |
| 6 | +# locator(selector[, options]) |
| 7 | + |
| 8 | +The method returns an element [Locator](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/). Locators resolve to the element when the action takes place, which means locators can span over navigations where the underlying dom changes. |
| 9 | + |
| 10 | +<TableWithNestedRows> |
| 11 | + |
| 12 | +| Parameter | Type | Default | Description | |
| 13 | +| ------------------- | ---------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 14 | +| selector | string | `''` | A selector to use when resolving DOM element. | |
| 15 | +| options | object | `null` | | |
| 16 | +| options.hasText | string or RegExp | `null` | Matches only elements that contain the specified text. String or regular expression. Optional. | |
| 17 | +| options.hasNotText | string or RegExp | `null` | Matches only elements that do not contain the specified text. String or regular expression. Optional. | |
| 18 | + |
| 19 | +</TableWithNestedRows> |
| 20 | + |
| 21 | +### Returns |
| 22 | + |
| 23 | +| Type | Description | |
| 24 | +| -------------------------------------------------------------------------------------- | ------------------------------------------------ | |
| 25 | +| [Locator](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/) | The element `Locator` associated with the frame. | |
| 26 | + |
| 27 | +### Example |
| 28 | + |
| 29 | +{{< code >}} |
| 30 | + |
| 31 | +```javascript |
| 32 | +import { browser } from 'k6/browser'; |
| 33 | + |
| 34 | +export const options = { |
| 35 | + scenarios: { |
| 36 | + ui: { |
| 37 | + executor: 'shared-iterations', |
| 38 | + options: { |
| 39 | + browser: { |
| 40 | + type: 'chromium', |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + }, |
| 45 | +}; |
| 46 | + |
| 47 | +export default async function () { |
| 48 | + const page = await browser.newPage(); |
| 49 | + |
| 50 | + await page.goto('https://quickpizza.grafana.com'); |
| 51 | + |
| 52 | + // Get a locator for an iframe element |
| 53 | + const frameLocator = page.locator('iframe').contentFrame(); |
| 54 | + |
| 55 | + // Create a locator within the frame with text filtering options |
| 56 | + const submitButton = frameLocator.locator('button', { hasText: 'Submit Order' }); |
| 57 | + await submitButton.click(); |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +{{< /code >}} |
0 commit comments