# -*- coding: utf-8 -*- """最小验证:wait_for_function(expression, arg=...) 在 file:// 页面是否抛异常。""" import io import sys import traceback sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') from playwright.sync_api import sync_playwright DEMO = 'file:///D:/code/crm-backend-matt/.scratch/opportunity-e2e/demo/index.html' with sync_playwright() as p: browser = p.chromium.launch(headless=True) page = browser.new_page() page.goto(DEMO) page.wait_for_timeout(1500) # 1) 不带 arg try: page.wait_for_function("() => document.getElementById('subBox') !== null", timeout=2000) print('no-arg: OK') except Exception as e: print('no-arg FAIL:', type(e).__name__, str(e)[:120]) # 2) 带 arg(与 wait_sub_has 相同形态) try: page.wait_for_function("text => document.getElementById('subBox').textContent.includes(text)", arg='xxx-不存在', timeout=2000) print('with-arg: OK(不应出现)') except Exception as e: print('with-arg 异常类型:', type(e).__name__) traceback.print_exc(limit=2) # 3) arg 为中文 try: page.wait_for_function("text => !document.getElementById('subBox').textContent.includes(text)", arg='中文参数', timeout=2000) print('cn-arg not-in: OK') except Exception as e: print('cn-arg 异常:', type(e).__name__, str(e)[:200]) browser.close()