#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 测试 playwright_stealth 修复 """ import asyncio from loguru import logger from playwright.async_api import async_playwright from playwright_stealth.stealth import Stealth async def test_stealth(): """测试 stealth 功能""" logger.info("开始测试 playwright_stealth 功能") try: # 测试 Stealth 类导入和实例化 stealth = Stealth() logger.success("Stealth 类实例化成功") # 检查 Stealth 类的方法 methods = [m for m in dir(stealth) if 'apply' in m.lower()] logger.info(f"Stealth 类包含的方法: {methods}") # 测试 Playwright 浏览器启动 logger.info("测试 Playwright 浏览器启动...") playwright = await async_playwright().start() # 启动浏览器 browser = await playwright.chromium.launch(headless=True) logger.success("Playwright 浏览器启动成功") # 创建页面上下文 context = await browser.new_context() page = await context.new_page() logger.success("页面创建成功") # 测试 stealth 应用 logger.info("测试应用 stealth 设置...") await stealth.apply_stealth_async(page) logger.success("stealth 设置应用成功") # 关闭浏览器 await browser.close() await playwright.stop() logger.success("浏览器关闭成功") logger.success("所有测试通过!playwright_stealth 修复成功") return True except Exception as e: logger.error(f"测试失败: {e}") return False async def main(): """主函数""" logger.info("=== playwright_stealth 修复测试 ===") success = await test_stealth() if success: logger.info("✅ 修复成功!您现在可以正常运行 new_data_stealth.py") logger.info("运行命令: python product/new_data_stealth.py") else: logger.error("❌ 修复失败,请检查错误信息") if __name__ == "__main__": asyncio.run(main())