fix hover action (#4245)

This commit is contained in:
LawyZheng
2025-12-10 02:39:17 +08:00
committed by GitHub
parent ff1dcfc0cf
commit 2de27637db
6 changed files with 33 additions and 72 deletions

View File

@@ -2024,30 +2024,6 @@ async def handle_hover_action(
await skyvern_element.get_locator().scroll_into_view_if_needed()
await skyvern_element.get_locator().hover(timeout=settings.BROWSER_ACTION_TIMEOUT_MS)
# Save the absolute page position of the hovered element
# This allows us to scroll back to this position after re-scraping
try:
bounding_box = await skyvern_element.get_locator().bounding_box(timeout=settings.BROWSER_ACTION_TIMEOUT_MS)
if bounding_box:
# Get current scroll position
scroll_y = await page.evaluate("window.scrollY")
# Calculate absolute page Y = viewport Y + scroll offset
absolute_page_y = bounding_box["y"] + scroll_y
context = skyvern_context.current()
if context:
context.last_hovered_element_page_y = absolute_page_y
context.last_hovered_element_id = action.element_id
LOG.info(
"Saved hovered element absolute position",
element_id=action.element_id,
viewport_y=bounding_box["y"],
scroll_y=scroll_y,
absolute_page_y=absolute_page_y,
)
except Exception:
LOG.warning("Failed to save hovered element position", exc_info=True)
if action.hold_seconds and action.hold_seconds > 0:
await asyncio.sleep(action.hold_seconds)
return [ActionSuccess()]
@@ -2348,7 +2324,6 @@ async def chain_click(
:param css: css of the element to click
"""
try:
await skyvern_element.hover_to_reveal()
if not await skyvern_element.navigate_to_a_href(page=page):
await locator.click(timeout=timeout)
LOG.info("Chain click: main element click succeeded", action=action, locator=locator)

View File

@@ -2,7 +2,6 @@ import asyncio
import copy
import json
from collections import defaultdict
from typing import Any
import structlog
from playwright._impl._errors import TimeoutError
@@ -93,14 +92,6 @@ def load_js_script() -> str:
JS_FUNCTION_DEFS = load_js_script()
# function to convert JSON element to HTML
def build_attribute(key: str, value: Any) -> str:
if isinstance(value, bool) or isinstance(value, int):
return f'{key}="{str(value).lower()}"'
return f'{key}="{str(value)}"' if value else key
def clean_element_before_hashing(element: dict) -> dict:
def clean_nested(element: dict) -> dict:
element_cleaned = {key: value for key, value in element.items() if key not in {"id", "rect", "frame_index"}}
@@ -328,6 +319,15 @@ async def scrape_web_unsafe(
if token_count > DEFAULT_MAX_TOKENS:
max_screenshot_number = min(max_screenshot_number, 1)
# get current x, y position of the page
x: int | None = None
y: int | None = None
try:
x, y = await skyvern_frame.get_scroll_x_y()
LOG.debug("Current x, y position of the page before scraping", x=x, y=y)
except Exception:
LOG.warning("Failed to get current x, y position of the page", exc_info=True)
screenshots = await SkyvernFrame.take_split_screenshots(
page=page,
url=url,
@@ -335,6 +335,12 @@ async def scrape_web_unsafe(
max_number=max_screenshot_number,
scroll=scroll,
)
# scroll back to the original x, y position of the page
if x is not None and y is not None:
await skyvern_frame.safe_scroll_to_x_y(x, y)
LOG.debug("Scrolled back to the original x, y position of the page after scraping", x=x, y=y)
id_to_css_dict, id_to_element_dict, id_to_frame_dict, id_to_element_hash, hash_to_element_ids = build_element_dict(
elements
)