Hello,
How can I iterate through a list of locators? all()
is not available and I am getting flagsBtn is not a iterable
const flagsBtn = page.locator('mat-button-toggle');
for (const [el, index] of flagsBtn) {
if (index !== 0) {
await el.click();
}
}
await expect(checkboxes).toHaveCount(13);
});
This thread is trying to answer question "How can one iterate through a list of CSS locators in Playwright when 'all()' is not working as expected?"
This is not working... see anything wrong, please? The icon buttons are not getting clicked
it.only('should have records', async ({ page }) => {
const flagsBtn = page.locator('mat-button-toggle').count();
for (index in flagsBtn) {
if (index !== 0) {
await page.locator('mat-button-toggle').nth(index).click();
}
}
await page.waitForTimeout(900000)
}); ```
it is for the html
<mat-button-toggle _ngcontent-ng-c2732246807="" role="presentation" data-testid="flag-btn" class="mat-button-toggle ng-tns-c2732246807-5 mat-button-toggle-standalone mat-button-toggle-appearance-standard" ng-reflect-checked="false" ng-reflect-value="[object Object]" id="mat-button-toggle-1"><button type="button" class="mat-button-toggle-button mat-focus-indicator" id="mat-button-toggle-1-button" tabindex="0" aria-pressed="false"><span class="mat-button-toggle-label-content"><mat-icon _ngcontent-ng-c2732246807="" role="img" class="mat-icon notranslate material-icons mat-ligature-font mat-icon-no-color" aria-hidden="true" data-mat-icon-type="font">flag</mat-icon></span></button><span class="mat-button-toggle-focus-overlay"></span><span matripple="" class="mat-ripple mat-button-toggle-ripple" ng-reflect-trigger="[object HTMLButtonElement]"></span></mat-button-toggle>
Is the destructuring to blame?
I think each element of btns is not an array, but the code [btn, index] of btns
is treating them like arrays.
The example from the playwright.dev website for iterating through all elements is also different:
for (const row of await page.getByRole('listitem').all())
console.log(await row.textContent());
Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].