How to use Playwright to simulate the click event on a canvas element?

2k Views Asked by At

When click everywhere on the canvas, the Playwright Inspector generates the same code:

page.click(":nth-match(canvas, 4)");

It can't trigger a event that happened on a button or a table line. Anybody could give me a solution to work out a automated test script on canvas?

1

There are 1 best solutions below

3
moefinley On

You can provide X Y coordinates with the options object of the click action.

await page.locator('canvas').click({
  button: 'right',
  modifiers: ['Shift'],
  position: { x: 23, y: 32 },
});

https://playwright.dev/docs/api/class-locator#locator-click