您的位置:首页 > 娱乐 > 明星 > 黑果云免费虚拟主机_深圳网络推广公司_定制网站+域名+企业邮箱_重庆百度推广开户

黑果云免费虚拟主机_深圳网络推广公司_定制网站+域名+企业邮箱_重庆百度推广开户

2025/4/10 7:26:28 来源:https://blog.csdn.net/qq_41472205/article/details/146555814  浏览:    关键词:黑果云免费虚拟主机_深圳网络推广公司_定制网站+域名+企业邮箱_重庆百度推广开户
黑果云免费虚拟主机_深圳网络推广公司_定制网站+域名+企业邮箱_重庆百度推广开户

目录

    • 代码
    • 代码解释
    • 执行效果

代码

import asyncio
import base64
import os
from datetime import datetimeimport pytestfrom browser_use.browser.browser import Browser, BrowserConfigasync def test_take_full_page_screenshot():browser = Browser(config=BrowserConfig(browser_instance_path=r"C:\Program Files\Google\Chrome\Application\chrome.exe",headless=False, disable_security=True))async with await browser.new_context() as context:page = await context.get_current_page()# Go to a test pageawait page.goto('https://www.google.com')await asyncio.sleep(3)# Take full page screenshotscreenshot_b64 = await context.take_screenshot(full_page=True)await asyncio.sleep(3)# 创建screenshots目录(如果不存在)screenshots_dir = os.path.join(os.path.dirname(__file__), 'screenshots')os.makedirs(screenshots_dir, exist_ok=True)# 生成带时间戳的文件名timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')screenshot_path = os.path.join(screenshots_dir, f'screenshot_{timestamp}.png')# 将base64字符串解码并保存为图片文件with open(screenshot_path, 'wb') as f:f.write(base64.b64decode(screenshot_b64))print(f'Screenshot saved to: {screenshot_path}')# Verify screenshot is not empty and is valid base64assert screenshot_b64 is not Noneassert isinstance(screenshot_b64, str)assert len(screenshot_b64) > 0# Test we can decode the base64 stringtry:base64.b64decode(screenshot_b64)except Exception as e:pytest.fail(f'Failed to decode base64 screenshot: {str(e)}')await browser.close()if __name__ == '__main__':asyncio.run(test_take_full_page_screenshot())

代码解释

  1. 导入必要模块
import asyncio  # 用于异步操作
import base64   # 用于处理base64编码的图片数据
import os       # 用于文件和目录操作
from datetime import datetime  # 用于生成时间戳
  1. 测试函数定义
async def test_take_full_page_screenshot():

这是一个异步测试函数,用于测试网页截图功能。

  1. 浏览器初始化
browser = Browser(config=BrowserConfig(browser_instance_path=r"C:\Program Files\Google\Chrome\Application\chrome.exe",headless=False,  # 显示浏览器界面disable_security=True))  # 禁用安全限制
  1. 页面操作
  • 创建新的浏览器上下文
  • 打开谷歌首页
  • 等待3秒让页面加载
  • 获取全页面截图(base64格式)
  1. 截图保存流程
# 创建保存目录
screenshots_dir = os.path.join(os.path.dirname(__file__), 'screenshots')
os.makedirs(screenshots_dir, exist_ok=True)# 生成文件名(使用时间戳)
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
screenshot_path = os.path.join(screenshots_dir, f'screenshot_{timestamp}.png')# 保存图片
with open(screenshot_path, 'wb') as f:f.write(base64.b64decode(screenshot_b64))
  1. 验证步骤
  • 检查截图数据不为空
  • 验证数据类型是字符串
  • 确保数据长度大于0
  • 测试能否正确解码base64数据
  1. 程序入口
if __name__ == '__main__':asyncio.run(test_take_full_page_screenshot())

使用 asyncio.run() 运行异步测试函数

这个脚本的主要目的是:

  1. 自动化测试网页截图功能
  2. 保存截图到本地文件
  3. 验证截图数据的有效性
  4. 提供可重复的测试用例

运行后会在脚本所在目录下创建 screenshots 文件夹,并保存带时间戳的PNG格式截图。

执行效果

python screenshot_test.py
INFO     [browser_use] BrowserUse logging setup complete with level info
INFO     [root] Anonymized telemetry enabled. See https://docs.browser-use.com/development/telemetry for more information.
Screenshot saved to: D:\llm\browser-use-test\browser-use\screenshots\screenshot_20250326_234705.png

请添加图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com