This commit is contained in:
@@ -175,6 +175,85 @@ async def click_selector_service(decoded_url: str, selector: str, return_type: s
|
||||
|
||||
return await safe_browser_operation(decoded_url, click_operation)
|
||||
|
||||
async def interactions_service(decoded_url: str, interactions, return_type: str):
|
||||
"""Open `decoded_url`, execute click/type interactions in order, then return screenshot or HTML."""
|
||||
print(f"Running interactions on {decoded_url} (return_type={return_type})")
|
||||
|
||||
async def interactions_operation(page):
|
||||
try:
|
||||
# Navigate and wait for initial DOM.
|
||||
await page.goto(decoded_url, wait_until="domcontentloaded", timeout=30000)
|
||||
await page.wait_for_load_state("load", timeout=30000)
|
||||
|
||||
# Ensure the page is fully loaded (helps with JS-driven UIs).
|
||||
await page.wait_for_function("document.readyState === 'complete'", timeout=30000)
|
||||
|
||||
# Best-effort cookie/banner handling (helps avoid click interception).
|
||||
cookie_buttons = [
|
||||
"Accept all",
|
||||
"Accepteer",
|
||||
"Accepteren",
|
||||
"Accept",
|
||||
]
|
||||
for button in cookie_buttons:
|
||||
try:
|
||||
element = await page.query_selector(f'text="{button}"')
|
||||
if element:
|
||||
await element.click()
|
||||
await page.wait_for_timeout(1000)
|
||||
break
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
for interaction in interactions:
|
||||
# Support both Pydantic model instances and raw dicts.
|
||||
if isinstance(interaction, dict):
|
||||
action = interaction.get("action")
|
||||
selector = interaction.get("selector")
|
||||
text = interaction.get("text")
|
||||
else:
|
||||
action = getattr(interaction, "action", None)
|
||||
selector = getattr(interaction, "selector", None)
|
||||
text = getattr(interaction, "text", None)
|
||||
|
||||
if not action or not selector:
|
||||
raise ValueError("Interaction must contain action and selector")
|
||||
|
||||
if action == "click":
|
||||
await page.wait_for_selector(selector, timeout=30000)
|
||||
locator = page.locator(selector)
|
||||
await locator.scroll_into_view_if_needed(timeout=30000)
|
||||
await locator.click(force=True, timeout=30000)
|
||||
|
||||
elif action == "type":
|
||||
if text is None:
|
||||
raise ValueError("Type interaction must contain text")
|
||||
await page.wait_for_selector(selector, timeout=30000)
|
||||
locator = page.locator(selector)
|
||||
await locator.scroll_into_view_if_needed(timeout=30000)
|
||||
await locator.fill(text)
|
||||
|
||||
else:
|
||||
raise ValueError(f"Unknown interaction action: {action}")
|
||||
|
||||
# Let the UI settle after each action.
|
||||
await page.wait_for_timeout(1000)
|
||||
try:
|
||||
await page.wait_for_load_state("networkidle", timeout=10000)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if return_type == "html":
|
||||
return await page.content()
|
||||
|
||||
return await page.screenshot(full_page=True, type="png")
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error during interactions capture: {e}")
|
||||
return {"status": "error", "url": decoded_url, "error": str(e)}
|
||||
|
||||
return await safe_browser_operation(decoded_url, interactions_operation)
|
||||
|
||||
async def extract_seo_service(decoded_url):
|
||||
"""Service function to extract SEO information from a website"""
|
||||
print(f"Extracting SEO from: {decoded_url}")
|
||||
|
||||
Reference in New Issue
Block a user