Move to the element before clicking it

This commit is contained in:
黄伟韬 2024-08-19 19:52:39 +08:00
parent 408174ed8f
commit fde0683f21

View file

@ -111,6 +111,12 @@ async def click_element(page: Page, backend_node_id: int):
resp = await get_bounding_rect(cdp_session, backend_node_id)
node_info = resp["result"]["value"]
x, y = await get_element_center(node_info)
# Move to the location of the element
await page.evaluate(f"window.scrollTo({x}- window.innerWidth/2,{y} - window.innerHeight/2);")
# Refresh the relative location of the element
resp = await get_bounding_rect(cdp_session, backend_node_id)
node_info = resp["result"]["value"]
x, y = await get_element_center(node_info)
await page.mouse.click(x, y)