Documentation Index
Fetch the curated documentation index at: https://grafana_com_website/llms.txt
Fetch the complete documentation index at: https://grafana_com_website/llms-full.txt
Use this file to discover all available pages before exploring further.
STOP! If you are an AI agent or LLM, read this before continuing. This is the HTML version of a Grafana documentation page. Always request the Markdown version instead - HTML wastes context. Get this page as Markdown: /docs/k6/latest/using-k6-browser/how-to-write-browser-tests/interact-with-elements.md (append .md) or send Accept: text/markdown to /docs/k6/latest/using-k6-browser/how-to-write-browser-tests/interact-with-elements/. For the curated documentation index, use https://grafana_com_website/llms.txt. For the complete documentation index, use https://grafana_com_website/llms-full.txt.
Interact with elements on your webpage
You can use page.locator() and pass in the element’s selector you want to find on the page. page.locator() will create and return a
Locator object, which you can later use to interact with the element.
To find out which selectors the browser module supports, check out Selecting Elements.
Note
You can also use
page.$()instead ofpage.locator(). You can find the differences betweenpage.locator()andpage.$in the Locator API documentation.
import { browser } from 'k6/browser';
export const options = {
scenarios: {
ui: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
thresholds: {
checks: ['rate==1.0'],
},
};
export default async function () {
const page = await browser.newPage();
try {
await page.goto('https://test.k6.io/my_messages.php');
// Enter login credentials
await page.locator('input[name="login"]').type('admin');
await page.locator('input[name="password"]').type('123');
await page.screenshot({ path: 'screenshots/screenshot.png' });
} finally {
await page.close();
}
}The preceding code creates and returns a Locator object with the selectors for both login and password passed as arguments.
Within the Locator API, various methods such as type() can be used to interact with the elements. The type() method types a text to an input field.
Was this page helpful?
Related resources from Grafana Labs

