"""调试脚本:启动浏览器,停在当前页面,打印 DOM 和截图,方便定位元素。""" import asyncio import os import sys _scripts_dir = os.path.dirname(os.path.abspath(__file__)) if _scripts_dir not in sys.path: sys.path.insert(0, _scripts_dir) from service.rpa.browser_session import start_browser_session, close_browser_context async def main(): profile_dir = os.environ.get("TEMP", "") + "\\verify-invoice-edge-profile" os.makedirs(profile_dir, exist_ok=True) pw, context, page = await start_browser_session(profile_dir, headless=False) # 打开 sandbox 首页 await page.goto("https://sandbox.jc2009.com/", wait_until="domcontentloaded", timeout=60000) await asyncio.sleep(5.0) print("=" * 60) print(f"当前 URL: {page.url}") print("=" * 60) # 打印所有可见文本 try: body = await page.locator("body").inner_text() print("\n--- 页面文本(前2000字)---") print(body[:2000]) except Exception as e: print(f"获取文本失败: {e}") # 打印所有 input 元素 print("\n--- 所有 input 元素 ---") inputs = page.locator("input") count = await inputs.count() for i in range(min(count, 20)): inp = inputs.nth(i) try: itype = await inp.get_attribute("type") or "" iname = await inp.get_attribute("name") or "" iid = await inp.get_attribute("id") or "" placeholder = await inp.get_attribute("placeholder") or "" value = await inp.input_value() print(f" [{i}] type={itype} name={iname} id={iid} placeholder={placeholder} value={value[:30]}") except: pass # 打印所有 button print("\n--- 所有 button ---") buttons = page.locator("button") bcount = await buttons.count() for i in range(min(bcount, 10)): btn = buttons.nth(i) try: text = await btn.inner_text() print(f" [{i}] text={text.strip()[:50]}") except: pass # 打印所有链接 print("\n--- 包含'智慧校园'的元素 ---") campus = page.get_by_text("智慧校园", exact=False) ccount = await campus.count() print(f" 找到 {ccount} 个") for i in range(min(ccount, 5)): try: text = await campus.nth(i).inner_text() tag = await campus.nth(i).evaluate("el => el.tagName") print(f" [{i}] tag={tag} text={text.strip()[:80]}") except: pass # 截图 ss_path = os.path.join(os.environ.get("TEMP", ""), "debug_sandbox_page.png") await page.screenshot(path=ss_path, full_page=True) print(f"\n截图已保存: {ss_path}") print("\n按回车关闭浏览器...") input() await close_browser_context(pw, context) if __name__ == "__main__": asyncio.run(main())