Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

experimental/browser: strict mode violation, multiple elements returned for selector query #4331

Open
luka-nikolic opened this issue May 23, 2024 · 2 comments

Comments

@luka-nikolic
Copy link

Brief summary

When multiple elements match selector and I want to execute click() on locator I get error: Uncaught (in promise) clicking on "[data-action="add-to-cart"][aria-label="In den Warenkorb"][data-quantity-input="#quantity"]": strict mode violation, multiple elements returned for selector query

Code example:

    const addToCartButton = page.locator('[data-action="add-to-cart"][aria-label="In den Warenkorb"]');
    await addToCartButton.click();

k6 version

v0.51.0

OS

Windows 10

Docker version and image (if applicable)

No response

Steps to reproduce the problem

export const options = {
  scenarios: {
    ui: {
      executor: 'shared-iterations',
      options: {
        browser: {
          type: 'chromium',
        },
      },
    },
  },
  thresholds: {
    checks: ["rate==1.0"]
  }
}

export default async function () {
  const page = browser.newPage({extraHTTPHeaders: HEADERS});

  try {
    await page.goto(BASE_URL);
    const addToCartButton = page.locator('[data-action="add-to-cart"][aria-label="In den Warenkorb"]');
    await addToCartButton.click();
  } finally {
    page.close();
  }
}

There are multiple elements on page that match selector [data-action="add-to-cart"][aria-label="In den Warenkorb"].
It fails when i execute click() method

Expected behaviour

.locator() should return only the first element that matches the selector (similar to how HTML DOM Document querySelector() works).

Actual behaviour

.locator() return multiple elements.

@joanlopez joanlopez assigned ankur22 and unassigned joanlopez May 23, 2024
@joanlopez joanlopez transferred this issue from grafana/k6 May 23, 2024
@nexus9111
Copy link

Is this fix ? I actually face this issue.

@ankur22
Copy link
Contributor

ankur22 commented Dec 12, 2024

@nexus9111, if there are multiple elements that match the selector, then you will see the "strict mode violation" error. You will need to ensure that the element can be uniquely identified with a unique selector to it.

@ankur22 ankur22 removed their assignment Jan 21, 2025
@inancgumus inancgumus transferred this issue from grafana/xk6-browser Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants