|
| 1 | +--- |
| 2 | +title: 'locator(selector)' |
| 3 | +description: 'Browser module: locator.locator method' |
| 4 | +--- |
| 5 | + |
| 6 | +# locator(selector) |
| 7 | + |
| 8 | +The method finds all elements matching the selector and creates a new [Locator](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/) that matches all of them. This method can be used to further refine the locator by chaining additional selectors. |
| 9 | + |
| 10 | +This allows you to define locators relative to a parent locator, enabling more precise element targeting by creating nested locators. |
| 11 | + |
| 12 | +### Returns |
| 13 | + |
| 14 | +| Type | Description | |
| 15 | +| -------------------------------------------------------------------------------------- | -------------------------------------------------------- | |
| 16 | +| [Locator](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-browser/locator/) | The new `Locator` associated with the selector. | |
| 17 | + |
| 18 | +### Example |
| 19 | + |
| 20 | +{{< code >}} |
| 21 | + |
| 22 | +```javascript |
| 23 | +import { expect } from 'https://jslib.k6.io/k6-testing/{{< param "JSLIB_TESTING_VERSION" >}}/index.js'; |
| 24 | +import { browser } from 'k6/browser'; |
| 25 | + |
| 26 | +export const options = { |
| 27 | + scenarios: { |
| 28 | + ui: { |
| 29 | + executor: 'shared-iterations', |
| 30 | + options: { |
| 31 | + browser: { |
| 32 | + type: 'chromium', |
| 33 | + }, |
| 34 | + }, |
| 35 | + }, |
| 36 | + }, |
| 37 | +}; |
| 38 | + |
| 39 | +export default async function () { |
| 40 | + const page = await browser.newPage(); |
| 41 | + |
| 42 | + try { |
| 43 | + await page.setContent(` |
| 44 | + <div> |
| 45 | + <div data-testid="products"> |
| 46 | + <div data-category="fruits"> |
| 47 | + <div data-product="apple"> |
| 48 | + <h3>Apple</h3> |
| 49 | + <button>Add to Cart</button> |
| 50 | + <button>View Details</button> |
| 51 | + </div> |
| 52 | + </div> |
| 53 | + <div data-category="fruits"> |
| 54 | + <h3>Orange</h3> |
| 55 | + <button>Add to Cart</button> |
| 56 | + <button>View Details</button> |
| 57 | + </div> |
| 58 | + <div data-category="vegetables"> |
| 59 | + <h3>Carrot</h3> |
| 60 | + <button>Add to Cart</button> |
| 61 | + <button>View Details</button> |
| 62 | + </div> |
| 63 | + </div> |
| 64 | + </div> |
| 65 | + `); |
| 66 | + |
| 67 | + // Use locator.locator to find specific products within the list |
| 68 | + const productList = page.locator('div[data-testid="products"]'); |
| 69 | + const fruitProducts = productList.locator('div[data-category="fruits"]'); |
| 70 | + const appleProduct = fruitProducts.locator('div[data-product="apple"]'); |
| 71 | + const addToCartButton = appleProduct.locator('//button[text()="Add to Cart"]'); |
| 72 | + |
| 73 | + // Interact with the nested locators |
| 74 | + await addToCartButton.click(); |
| 75 | + } finally { |
| 76 | + await page.close(); |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +{{< /code >}} |
0 commit comments