Nir Berko
Nir Berko

Reputation: 1428

playwright get click target

is it possible to get the target on click? for example, I have the following code:

await page.mouse.click(200, 200)

and in this position I have an input, is it possible to get the target in this clicked position and find out that there is a input there?

Upvotes: 1

Views: 11236

Answers (1)

arjunattam
arjunattam

Reputation: 2819

You can pass the coordinates to the document.elementFromPoint method (MDN docs). Since this method is in the web page context, we will use page.evaluate in the Playwright API.

await page.evaluate(([x, y]) => {
  const element = document.elementFromPoint(x, y);
  return element instanceof HTMLInputElement;
}, [200, 200])

The x, y values in the page.mouse.click are relative to the top-left corner of the viewport, which is what the elementFromPoint API expects.

Upvotes: 4

Related Questions