Locator multiple elements returned for selector query

try {
  await page.locator('button.bp-button').click();
  // await page.locator('button.bp-button[name='Login']").click();
} catch (error) {
   console.log("error:" + error);
   exec.test.abort();
}

returns multiple elements, how can I choose the last one?

INFO[0012] error:clicking on "button.bp-button": strict mode violation, multiple elements returned for selector query  source=console

It is a shadow-root so I cant use xpath only css_selector or text:

Selenium has a own shadow_root attribute:

el = wd.find_element(By.XPATH, "//bp-button[@cancel='false']")
el.shadow_root.find_element(By.CSS_SELECTOR, "button").click()

In playwright recorder returned this:

page.get_by_role("button", name="Login").click()

I’ve tried various locators but no luck

Hi @mortenb123,

The current Locator API doesn’t support multiple elements. You can use page.$$ instead. The downside of using page.$$ is that it contains a reference to the actual elements (ElementHandles) on the page. On the other hand, a locator queries at the time you want to find an element—this makes a locator work across navigations. Because of this, you will need to call page.$$ again after each navigation.

Hope this helps.