Playwright force click

The “playwright force click” is a feature provided by the Playwright library that allows you to forcefully click an element on a web page, even if it’s not visible or interactable.

Here’s an example of how you can use the “force click” feature in Playwright:


const { chromium } = require('playwright');

(async () => {
  const browser = await chromium.launch();
  const context = await browser.newContext();
  const page = await context.newPage();

  await page.goto('https://www.example.com');

  // Forcefully click an element using its selector
  await page.$eval('#element-id', (element) => {
    element.click({ force: true });
  });

  await browser.close();
})();
  

In the above example, we first launch a Chromium browser and create a new page. Then, we navigate to a webpage using the page.goto function.

The page.$eval function allows us to evaluate the provided function in the context of the first matching element on the page based on the provided selector (#element-id in this case). Inside this function, we call the click method on the element and pass { force: true } as an option to forcefully perform the click action.

With this, the element will be clicked, regardless of its visibility or interactability. You can adjust the selector (#element-id) with the appropriate selector for your use case.

Leave a comment