admin管理员组

文章数量:1406926

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?

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?

Share Improve this question asked Jun 5, 2020 at 15:21 Nir BerkoNir Berko 1,4364 gold badges16 silver badges36 bronze badges 3
  • Is there any reason you rather click the position instead of the input itself? – innis Commented Jun 5, 2020 at 15:46
  • @innis just for learning :) – Nir Berko Commented Jun 5, 2020 at 15:51
  • I can't think in any way to do this rather than set an variable and the set an event listener for each one of your inputs and then define the previously declared variable with the value you want to set when you click. – innis Commented Jun 5, 2020 at 22:54
Add a ment  | 

1 Answer 1

Reset to default 4

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.

本文标签: javascriptplaywright get click targetStack Overflow