question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Is there way to iterate the Locator elements?

See original GitHub issue

Hi all,

I would like to iterate the Locator elements while using the ‘of’ operator. For example, if I found several same elements with a page.locator function - is there way to check each of them through the loop iteration:

let arrayOfLocators = page.locator('element);
for (let singleLocator of arrayOfLocators) {
    const textOfElement = await singleLocator.innerText();
}

Seems like it is not possible with Locator elements and can be very useful!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:8
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

38reactions
0xIslamTahacommented, Feb 17, 2022

@DanielStoica85 I managed to solve it with the following code

test('user can check multiple radio ', async ({ page }) => {
  const radios = await page.locator('[type="radio"]');
  const count = await radios.count();
  for (let i = 0; i < count; i++) {
    await radios.nth(i).click();
  }
})
25reactions
DetachHeadcommented, Jul 13, 2022

you could make a generator

async function* iterateLocator(locator: Locator): AsyncGenerator<Locator> {
    for (let index = 0; index < await locator.count(); index++) {
        yield locator.nth(index)
    }
}

now you can iterate over them with a for await loop:

for await (const locator of iterateLocator(page.locator('some-selector'))) {
    console.log(locator)
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there a way to iterate over a <li> list in Playwright and click ...
Since https://playwright.dev/docs/api/class-locator#locator-element-handles doesn't have a good example on how to use .elementHandles() .
Read more >
how to loop find elements in selenium webdriver - YouTube
In this video we will learn how to iterate find elements in selenium. FindElements will return list of web elements #FindElementsInSelenium ...
Read more >
Loop through list of elements and perform a button click
findElement(By.xpath("//div[@class='promotions-text']/parent::div[@class='promotions-block']/following-sibling::div[@class='product-catalog']/ ...
Read more >
Locating multiple elements in Selenium Python - GeeksforGeeks
Selenium Python follows different locating strategies for elements. One can locate multiple elements in 7 different ways. Here is a list of ...
Read more >
Locators in Selenium: A Detailed Guide - BrowserStack
In this tutorial, we have discussed methods that locate only single elements. One may want to select a group of elements and then...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found