""" Async example integrating agent-permissions with Playwright. Prerequisites: pip install playwright agent-permissions playwright install chromium """ from __future__ import annotations import asyncio from lxml import html from agent_permissions import async_api from agent_permissions.validation import validate_interaction async def run() -> None: try: from playwright.async_api import async_playwright except ImportError: # pragma: no cover - sample only raise SystemExit("Install playwright to run this example: pip install playwright") async with async_playwright() as p: browser = await p.chromium.launch() page = await browser.new_page() await page.goto("https://example.com/products") manifest_wrapper = await async_api.discover_manifest( page.url, html=await page.content(), ) if manifest_wrapper and manifest_wrapper.manifest: dom = html.fromstring(await page.content()) element = dom.cssselect(".cta-link")[0] result = validate_interaction( manifest_wrapper.manifest, verb="follow_link", element=element, ) if result.allowed: await page.click(".cta-link") print("Link followed in compliance with manifest.") else: print(f"Manifest denied link follow: {result.reasons}") else: print("No manifest discovered; skipping automated navigation.") await browser.close() if __name__ == "__main__": asyncio.run(run())