【截图】selenium自动通过浏览器截取指定元素div的图片

【截图】selenium自动通过浏览器截取指定元素div的图片

思路

截取完整网页截图

通过元素的坐标 截图到指定位置的图片

前提是已经获取到 driver 了

python 复制代码
    # 定位目标div
    target_div = driver.find_element(By.CLASS_NAME, 'headlines-right')

    # 获取div的位置和大小
    location = target_div.location
    size = target_div.size


    # driver.switch_to.alert.accept()
    full_screenshot_path = 'full_screenshot.png'
    driver.save_screenshot(full_screenshot_path)

    # 根据div的位置和大小裁剪图片

    # 根据div的位置和大小裁剪图片
    image = Image.open(full_screenshot_path)

    left = location['x']
    top = location['y']
    right = location['x'] + size['width']
    bottom = location['y'] + size['height']

    target_image = image.crop((left, top, right, bottom))
    target_image.save('div_screenshot.png')

    print("目标div的截图已保存为 div_screenshot.png")
相关推荐
坐吃山猪3 小时前
Python22-PostMan调试MCP
测试工具·postman·mcp
测试界的小码农1 天前
【 AI Agent 软件测试实战(05):任务规划与状态管理,让 Agent 学会拆解复杂测试任务】
人工智能·测试工具
和粒科技3 天前
热重分析仪TG 309 Libra Classic
测试工具
ClouGence4 天前
不搭 Selenium 或 Playwright,怎么测试公司的内网业务?
selenium·测试
螺丝钉tina9 天前
[Playwright] [02]常用命令与调试:Debug UI、Mode和 Trace Viewer
开发语言·javascript·测试工具·ui·自动化
梧桐16810 天前
14天、31个缺陷、28个测试脚本—用 Claude Code 完成第三方软件测试
人工智能·测试工具
mewowaBc10 天前
postman使用细则
测试工具·postman·偏差
光影62711 天前
Unittest单元测试基础速记
测试工具·单元测试·json·测试覆盖率
xie8810180211 天前
正则里的点号不匹配换行:多行模式的开关
运维·服务器·测试工具
2601_9679873712 天前
Python在自动化测试中有哪些应用?
python自动化测试·selenium·appium·pytest·性能测试