This is documentation for the next version of Grafana k6 documentation. For the latest stable release, go to the latest version.
getByLabel(text[, options])
Returns a locator for form controls associated with the specified label text. This method is ideal for interacting with form elements in an accessible and user-focused way, as it mirrors how users typically identify form fields.
Returns
Examples
Basic form interaction
Fill form fields using their labels:
Working with different input types
Handle various form control types in various label association patterns:
Label association patterns
The getByLabel() method works with several HTML patterns for associating labels with form controls:
Explicit association with
forattribute:<label for="username">Username</label> <input type="text" id="username" name="username" />ARIA labeling:
<span id="username-label">Username</span> <input type="text" aria-labelledby="username-label" />ARIA label attribute:
<input type="text" aria-label="Username" />
Label association patterns
The getByLabel() method works with several HTML patterns for associating labels with form controls:
Explicit association with
forattribute:<label for="username">Username</label> <input type="text" id="username" name="username" />ARIA labeling:
<span id="username-label">Username</span> <input type="text" aria-labelledby="username-label" />ARIA label attribute:
<input type="text" aria-label="Username" />
Common use cases
- Form testing: Login forms, registration forms, contact forms
 - E-commerce: Checkout forms, shipping information, payment details
 - Settings pages: User preferences, account settings, configuration forms
 - Accessibility testing: Ensuring proper label association and screen reader compatibility
 
Best practices
- Accessibility-first approach: Using 
getByLabel()ensures your tests work the same way users with assistive technology interact with forms. - Meaningful labels: Encourage developers to use descriptive, unique label text that clearly identifies the form control’s purpose.
 - Required field indicators: When testing required fields, include any visual indicators (like asterisks) in your label text matching.
 - Form validation testing: Use labels to test form validation scenarios, as they provide a stable way to identify fields regardless of styling changes.
 
Related
- page.getByRole() - Locate by ARIA role
 - page.getByAltText() - Locate by alt text
 - page.getByPlaceholder() - Locate by placeholder text
 - page.getByTestId() - Locate by test ID
 - page.getByTitle() - Locate by title attribute
 - page.getByText() - Locate by text content
 


