baseClassTools_examples.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. """
  2. baseClassTools.py 使用示例
  3. Usage Examples for baseClassTools.py
  4. 本文件演示如何使用基于 browser-use 原生类的工具集。
  5. """
  6. import asyncio
  7. from tools.baseClassTools import (
  8. # 会话管理
  9. init_browser_session,
  10. cleanup_browser_session,
  11. # 工具函数
  12. navigate_to_url,
  13. click_element,
  14. input_text,
  15. send_keys,
  16. scroll_page,
  17. get_page_html,
  18. get_selector_map,
  19. extract_content,
  20. wait,
  21. wait_for_user_action,
  22. search_web,
  23. screenshot,
  24. evaluate,
  25. )
  26. # ============================================================
  27. # 示例 1: 基础使用 - 简单的网页导航和交互
  28. # ============================================================
  29. async def example_1_basic_usage():
  30. """
  31. 基础示例:导航到网页,获取页面信息
  32. """
  33. print("\n" + "="*60)
  34. print("示例 1: 基础使用")
  35. print("="*60 + "\n")
  36. try:
  37. # 1. 初始化浏览器会话(只需要调用一次)
  38. await init_browser_session(
  39. headless=False,
  40. profile_name="example_profile"
  41. )
  42. # 2. 导航到网页
  43. result = await navigate_to_url("https://www.baidu.com")
  44. print(f"✅ 导航结果: {result.output}")
  45. # 3. 等待页面加载
  46. await wait(seconds=2)
  47. # 4. 获取页面 HTML
  48. html_result = await get_page_html()
  49. print(f"✅ 获取到 HTML,长度: {len(html_result.metadata.get('html', ''))}")
  50. # 5. 获取页面元素映射
  51. selector_result = await get_selector_map()
  52. print(f"✅ 找到元素: {selector_result.output.split('\\n')[0]}")
  53. finally:
  54. # 6. 清理会话
  55. await cleanup_browser_session()
  56. print("\n✅ 浏览器会话已清理")
  57. # ============================================================
  58. # 示例 2: 搜索和数据提取
  59. # ============================================================
  60. async def example_2_search_and_extract():
  61. """
  62. 搜索示例:使用搜索引擎搜索并提取结果
  63. """
  64. print("\n" + "="*60)
  65. print("示例 2: 搜索和数据提取")
  66. print("="*60 + "\n")
  67. try:
  68. # 1. 初始化浏览器
  69. await init_browser_session(headless=False)
  70. # 2. 使用 Google 搜索
  71. search_result = await search_web(
  72. query="Python async programming",
  73. engine="google"
  74. )
  75. print(f"✅ 搜索完成: {search_result.long_term_memory}")
  76. # 3. 等待结果加载
  77. await wait(seconds=3)
  78. # 4. 滚动查看更多结果
  79. await scroll_page(down=True, pages=1.0)
  80. print("✅ 滚动页面完成")
  81. # 5. 截图查看页面状态
  82. await screenshot()
  83. print("✅ 截图请求已发送")
  84. # 6. 获取页面 HTML 用于分析
  85. html_result = await get_page_html()
  86. url = html_result.metadata.get('url', '')
  87. print(f"✅ 当前页面: {url}")
  88. finally:
  89. await cleanup_browser_session()
  90. # ============================================================
  91. # 示例 3: 表单填写和提交
  92. # ============================================================
  93. async def example_3_form_interaction():
  94. """
  95. 表单交互示例:填写表单并提交
  96. """
  97. print("\n" + "="*60)
  98. print("示例 3: 表单填写和提交")
  99. print("="*60 + "\n")
  100. try:
  101. # 1. 初始化浏览器
  102. await init_browser_session(headless=False)
  103. # 2. 导航到表单页面(这里用百度作为示例)
  104. await navigate_to_url("https://www.baidu.com")
  105. await wait(seconds=2)
  106. # 3. 获取元素映射
  107. selector_result = await get_selector_map()
  108. print(f"✅ {selector_result.output.split(chr(10))[0]}")
  109. # 4. 找到搜索框并输入文本(假设索引为 1)
  110. # 注意:实际使用时需要查看 selector_result 找到正确的索引
  111. await input_text(index=1, text="browser automation", clear=True)
  112. print("✅ 输入文本完成")
  113. # 5. 发送回车键提交
  114. await send_keys(keys="Enter")
  115. print("✅ 按下回车键")
  116. # 6. 等待搜索结果加载
  117. await wait(seconds=3)
  118. # 7. 滚动查看结果
  119. await scroll_page(down=True, pages=2.0)
  120. print("✅ 滚动完成")
  121. finally:
  122. await cleanup_browser_session()
  123. # ============================================================
  124. # 示例 4: 需要登录的场景
  125. # ============================================================
  126. async def example_4_login_scenario():
  127. """
  128. 登录场景示例:导航到需要登录的网站,等待用户登录
  129. """
  130. print("\n" + "="*60)
  131. print("示例 4: 需要登录的场景")
  132. print("="*60 + "\n")
  133. try:
  134. # 1. 初始化浏览器(使用持久化配置保存登录状态)
  135. await init_browser_session(
  136. headless=False,
  137. profile_name="xiaohongshu_profile" # 使用专门的配置文件
  138. )
  139. # 2. 导航到小红书
  140. await navigate_to_url("https://www.xiaohongshu.com")
  141. await wait(seconds=2)
  142. # 3. 检查是否需要登录
  143. html_result = await get_page_html()
  144. html = html_result.metadata.get('html', '')
  145. if "登录" in html or "login" in html.lower():
  146. print("⚠️ 检测到需要登录")
  147. # 4. 等待用户手动登录
  148. wait_result = await wait_for_user_action(
  149. message="请在浏览器中登录小红书 (Please login to Xiaohongshu)",
  150. timeout=180 # 3分钟超时
  151. )
  152. if "完成" in wait_result.title:
  153. print("✅ 用户已完成登录")
  154. else:
  155. print("⚠️ 等待超时,继续执行")
  156. else:
  157. print("✅ 已经登录或不需要登录")
  158. # 5. 继续执行任务(这里只是示例)
  159. await wait(seconds=2)
  160. print("✅ 可以继续执行后续任务")
  161. # 注意:第二次运行时,由于使用了持久化配置,
  162. # 浏览器会自动加载之前保存的登录状态
  163. finally:
  164. # 不要立即清理,保持登录状态
  165. await cleanup_browser_session()
  166. print("\n✅ 会话已保存,下次运行会自动登录")
  167. # ============================================================
  168. # 示例 5: JavaScript 执行和高级操作
  169. # ============================================================
  170. async def example_5_javascript_execution():
  171. """
  172. JavaScript 执行示例:使用 JavaScript 进行高级操作
  173. """
  174. print("\n" + "="*60)
  175. print("示例 5: JavaScript 执行")
  176. print("="*60 + "\n")
  177. try:
  178. # 1. 初始化浏览器
  179. await init_browser_session(headless=False)
  180. # 2. 导航到网页
  181. await navigate_to_url("https://www.baidu.com")
  182. await wait(seconds=2)
  183. # 3. 执行 JavaScript 获取页面信息
  184. js_code = """
  185. (function(){
  186. try {
  187. return {
  188. title: document.title,
  189. url: window.location.href,
  190. links: document.querySelectorAll('a').length,
  191. images: document.querySelectorAll('img').length
  192. };
  193. } catch(e) {
  194. return 'Error: ' + e.message;
  195. }
  196. })()
  197. """
  198. result = await evaluate(code=js_code)
  199. print(f"✅ JavaScript 执行结果:\n{result.output}")
  200. # 4. 使用 JavaScript 滚动到特定位置
  201. scroll_js = "window.scrollTo(0, document.body.scrollHeight / 2)"
  202. await evaluate(code=scroll_js)
  203. print("✅ 使用 JS 滚动完成")
  204. # 5. 使用 JavaScript 提取数据
  205. extract_js = """
  206. (function(){
  207. const links = Array.from(document.querySelectorAll('a'));
  208. return links.slice(0, 5).map(a => ({
  209. text: a.textContent.trim(),
  210. href: a.href
  211. }));
  212. })()
  213. """
  214. links_result = await evaluate(code=extract_js)
  215. print(f"✅ 提取链接:\n{links_result.output[:200]}...")
  216. finally:
  217. await cleanup_browser_session()
  218. # ============================================================
  219. # 示例 6: 在 Agent 类中集成使用
  220. # ============================================================
  221. class MyBrowserAgent:
  222. """
  223. 示例 Agent 类:展示如何在自己的 Agent 中集成 baseClassTools
  224. """
  225. def __init__(self, profile_name: str = "default"):
  226. self.profile_name = profile_name
  227. self.initialized = False
  228. async def initialize(self):
  229. """初始化 Agent 和浏览器会话"""
  230. if not self.initialized:
  231. await init_browser_session(
  232. headless=False,
  233. profile_name=self.profile_name
  234. )
  235. self.initialized = True
  236. print("✅ Agent 已初始化")
  237. async def cleanup(self):
  238. """清理资源"""
  239. if self.initialized:
  240. await cleanup_browser_session()
  241. self.initialized = False
  242. print("✅ Agent 已清理")
  243. async def navigate_and_get_info(self, url: str):
  244. """导航到 URL 并获取页面信息"""
  245. await self.initialize()
  246. # 导航
  247. await navigate_to_url(url)
  248. await wait(seconds=2)
  249. # 获取信息
  250. html_result = await get_page_html()
  251. title = html_result.metadata.get('title', '')
  252. url = html_result.metadata.get('url', '')
  253. return {
  254. "title": title,
  255. "url": url
  256. }
  257. async def search_and_click(self, query: str, element_index: int):
  258. """搜索并点击指定元素"""
  259. await self.initialize()
  260. # 搜索
  261. await search_web(query=query, engine="google")
  262. await wait(seconds=3)
  263. # 点击元素
  264. await click_element(index=element_index)
  265. await wait(seconds=2)
  266. return True
  267. async def extract_with_login(self, url: str, need_login: bool = False):
  268. """提取数据,如果需要则等待登录"""
  269. await self.initialize()
  270. # 导航
  271. await navigate_to_url(url)
  272. await wait(seconds=2)
  273. # 如果需要登录
  274. if need_login:
  275. html_result = await get_page_html()
  276. html = html_result.metadata.get('html', '')
  277. if "登录" in html or "login" in html.lower():
  278. print("⚠️ 检测到需要登录")
  279. await wait_for_user_action("请登录", timeout=180)
  280. # 获取页面内容
  281. html_result = await get_page_html()
  282. return html_result.metadata.get('html', '')
  283. async def example_6_agent_integration():
  284. """
  285. Agent 集成示例:展示如何在 Agent 中使用工具
  286. """
  287. print("\n" + "="*60)
  288. print("示例 6: Agent 集成")
  289. print("="*60 + "\n")
  290. agent = MyBrowserAgent(profile_name="agent_profile")
  291. try:
  292. # 1. 导航并获取信息
  293. info = await agent.navigate_and_get_info("https://www.baidu.com")
  294. print(f"✅ 页面信息: {info}")
  295. # 2. 搜索并交互
  296. # await agent.search_and_click("Python", element_index=5)
  297. # print("✅ 搜索和点击完成")
  298. finally:
  299. await agent.cleanup()
  300. # ============================================================
  301. # 运行所有示例
  302. # ============================================================
  303. async def run_all_examples():
  304. """运行所有示例"""
  305. print("\n" + "="*80)
  306. print("开始运行 baseClassTools.py 使用示例")
  307. print("="*80)
  308. # 选择要运行的示例(取消注释来运行)
  309. await example_1_basic_usage()
  310. # await example_2_search_and_extract()
  311. # await example_3_form_interaction()
  312. # await example_4_login_scenario()
  313. # await example_5_javascript_execution()
  314. # await example_6_agent_integration()
  315. print("\n" + "="*80)
  316. print("所有示例运行完成")
  317. print("="*80 + "\n")
  318. if __name__ == "__main__":
  319. # 运行示例
  320. asyncio.run(run_all_examples())