Puppeteer 是否支持通过页面上呈现的坐标位置来选择元素?
Does Puppeteer support selecting elements by rendered coordinate location on a page?
我想要呈现一个页面和 运行 一种算法来直观地确定哪些位置具有我想要 select 属性的元素。 Puppeteer seems to support outputting a screenshot of the page and mouse move events by (x, y) 但我没有看到任何 selecting 元素以供 (x, y) 检查的方法。
有没有一种方法可以确定页面中的某个元素位于某个坐标?堆叠元素对我的用例来说不是问题,因为此时应该可以通过 xpath 访问所需的元素。
如果不直接支持,使用是否可行 mouse.move(x, y[, options]) to move the cursor to the desired position and somehow "tag" the element beneath the cursor with an predefined searchable attribute (ex: <element treasure=yes>
) that can be found with xpath? I am thinking page.evaluate(pageFunction[, ...args]) 可以用来注入一个 javascript 函数来动态标记光标下方的当前元素。
您可以使用 DocumentOrShadowRoot.elementFromPoint()
轻松做到这一点
await page.evaluateHandle((x,y) => document.elementFromPoint(x,y), x,y);
这将为您提供该坐标中最顶端的元素。
我想要呈现一个页面和 运行 一种算法来直观地确定哪些位置具有我想要 select 属性的元素。 Puppeteer seems to support outputting a screenshot of the page and mouse move events by (x, y) 但我没有看到任何 selecting 元素以供 (x, y) 检查的方法。
有没有一种方法可以确定页面中的某个元素位于某个坐标?堆叠元素对我的用例来说不是问题,因为此时应该可以通过 xpath 访问所需的元素。
如果不直接支持,使用是否可行 mouse.move(x, y[, options]) to move the cursor to the desired position and somehow "tag" the element beneath the cursor with an predefined searchable attribute (ex: <element treasure=yes>
) that can be found with xpath? I am thinking page.evaluate(pageFunction[, ...args]) 可以用来注入一个 javascript 函数来动态标记光标下方的当前元素。
您可以使用 DocumentOrShadowRoot.elementFromPoint()
await page.evaluateHandle((x,y) => document.elementFromPoint(x,y), x,y);
这将为您提供该坐标中最顶端的元素。