test_py311_browser_use.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #!/usr/bin/env python3
  2. """
  3. 使用 Python 3.11 + browser-use 测试
  4. """
  5. import asyncio
  6. import json
  7. from pathlib import Path
  8. from datetime import datetime
  9. async def test_browser_use():
  10. """测试 browser-use 基本功能"""
  11. print("="*60)
  12. print("🧪 测试 browser-use (Python 3.11)")
  13. print("="*60)
  14. print()
  15. from browser_use import Tools
  16. from browser_use.browser import BrowserSession
  17. try:
  18. # 创建浏览器会话
  19. print("📌 步骤 1: 创建浏览器会话...")
  20. browser = BrowserSession(
  21. headless=False,
  22. disable_security=True
  23. )
  24. # 启动浏览器
  25. print("📌 步骤 2: 启动浏览器...")
  26. await browser.start()
  27. print("✅ 浏览器已启动\n")
  28. # 创建工具实例
  29. tools = Tools()
  30. # 导航到百度
  31. print("📌 步骤 3: 导航到百度...")
  32. result = await tools.navigate(
  33. url="https://www.baidu.com",
  34. browser_session=browser
  35. )
  36. print(f"✅ {result.long_term_memory}\n")
  37. await asyncio.sleep(2)
  38. # 搜索
  39. search_keyword = "Python 教程"
  40. search_url = f"https://www.baidu.com/s?wd={search_keyword}"
  41. print(f"📌 步骤 4: 搜索 '{search_keyword}'...")
  42. result = await tools.navigate(
  43. url=search_url,
  44. browser_session=browser
  45. )
  46. print(f"✅ {result.long_term_memory}\n")
  47. await asyncio.sleep(3)
  48. # 滚动页面
  49. print("📌 步骤 5: 滚动页面...")
  50. await tools.scroll(
  51. down=True,
  52. pages=1.0,
  53. browser_session=browser
  54. )
  55. await asyncio.sleep(2)
  56. print("✅ 页面滚动完成\n")
  57. # 提取数据
  58. print("📌 步骤 6: 提取搜索结果...")
  59. extract_js = """
  60. (function(){
  61. try {
  62. const results = [];
  63. const resultItems = document.querySelectorAll('#content_left > div[class*="result"]');
  64. resultItems.forEach((item, index) => {
  65. if (index >= 10) return;
  66. const titleEl = item.querySelector('h3 a, .t a');
  67. const title = titleEl ? titleEl.textContent.trim() : '';
  68. const link = titleEl ? titleEl.href : '';
  69. const summaryEl = item.querySelector('.c-abstract, .content-right_8Zs40');
  70. const summary = summaryEl ? summaryEl.textContent.trim() : '';
  71. const sourceEl = item.querySelector('.c-color-gray, .source_1Vdff');
  72. const source = sourceEl ? sourceEl.textContent.trim() : '';
  73. if (title || link) {
  74. results.push({
  75. index: index + 1,
  76. title: title,
  77. link: link,
  78. summary: summary.substring(0, 200),
  79. source: source
  80. });
  81. }
  82. });
  83. return {
  84. success: true,
  85. count: results.length,
  86. keyword: 'Python 教程',
  87. timestamp: new Date().toISOString(),
  88. results: results
  89. };
  90. } catch (e) {
  91. return {
  92. success: false,
  93. error: e.message
  94. };
  95. }
  96. })()
  97. """
  98. result = await tools.evaluate(
  99. code=extract_js,
  100. browser_session=browser
  101. )
  102. # 解析结果
  103. output = result.extracted_content or str(result.metadata)
  104. if isinstance(output, str) and output.startswith("Result: "):
  105. output = output[8:]
  106. data = json.loads(output) if isinstance(output, str) else output
  107. if data.get('success'):
  108. print(f"✅ 成功提取 {data.get('count', 0)} 条结果\n")
  109. # 保存数据
  110. json_file = Path("baidu.json")
  111. with open(json_file, 'w', encoding='utf-8') as f:
  112. json.dump(data, f, ensure_ascii=False, indent=2)
  113. print(f"✅ 数据已保存: {json_file}\n")
  114. # 显示前3条结果
  115. if data.get('results'):
  116. print("📋 前3条结果:")
  117. for item in data['results'][:3]:
  118. print(f" {item.get('index')}. {item.get('title', '无标题')[:50]}...")
  119. print()
  120. else:
  121. print(f"⚠️ 提取失败: {data.get('error')}\n")
  122. # 保存 HTML
  123. print("📌 步骤 7: 保存页面 HTML...")
  124. cdp = await browser.get_or_create_cdp_session()
  125. html_result = await cdp.cdp_client.send.Runtime.evaluate(
  126. params={'expression': 'document.documentElement.outerHTML'},
  127. session_id=cdp.session_id
  128. )
  129. html_content = html_result.get('result', {}).get('value', '')
  130. html_file = Path("baidu_page.html")
  131. with open(html_file, 'w', encoding='utf-8') as f:
  132. f.write(f"<!-- 保存时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} -->\n")
  133. f.write(html_content)
  134. print(f"✅ HTML 已保存: {html_file}")
  135. print(f" 大小: {len(html_content):,} 字符\n")
  136. print("="*60)
  137. print("🎉 测试成功!")
  138. print("="*60)
  139. print("✅ browser-use 在 Python 3.11 中正常工作")
  140. print("✅ 生成文件:")
  141. print(" • baidu.json")
  142. print(" • baidu_page.html")
  143. print("="*60)
  144. # 停止浏览器
  145. await browser.stop()
  146. except Exception as e:
  147. print(f"\n❌ 错误: {e}")
  148. import traceback
  149. traceback.print_exc()
  150. if __name__ == "__main__":
  151. asyncio.run(test_browser_use())