Skip to content

Iterating over a list of elements and .click()-to-remove each. This is what worked for me #21410

Closed

Description

I realized it wasn't possible to do a for await (item of await listItemLocator.all()) to click each item in the list, when the purpose is to delete the item.
The reason for this is that Playwright internally uses nth(0), nth(1)... to click elements. And if the button deletes the element at nth(0) then nth(1), if there are two elements, will not be there.
So a recursive function is the solution:

async function removePills(pills: Locator) {
    const pillButtons = await pills.all();
    if (pillButtons.length > 0) {
        await pillButtons[0].click();
        await removePills(pills);
    }
}
await removePills(
    this.iframe.getByTestId("pw-provider-pill").getByRole("button")
);

Originally posted by @jkohlin in #2034 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions