【截图】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")
相关推荐
测试员周周2 小时前
【免费福利】AI测试:测试技能包进阶:造数、压测、视觉回归、CI 全流程串联
开发语言·人工智能·python·功能测试·测试工具·ci/cd·测试用例
PhotonixBay5 小时前
表面粗糙度怎么测才准?ISO 25178标准三维面积测量标准解析
人工智能·测试工具
KK溜了溜了6 小时前
wireshark and tcpdump抓包
测试工具·wireshark·tcpdump
老神在在0011 天前
JSON Schema
测试工具·json
小杍随笔1 天前
【FlyEnv v4.15.0 重磅更新!新增 FrankenPHP、CliProxyAPI、Numa、Rnacos 模块,开发效率再升级】
测试工具·开发环境管理工具
软件测试慧姐1 天前
软件测试常见面试题汇总(2026版)
软件测试·测试工具·面试
weixin_419658312 天前
基于 Trae 和 Postman-MCP-Server 接口自动化测试
测试工具·postman
weixin_419658313 天前
Postman-MCP-Server 的构建以及在 Trae 中的配置方法
测试工具·postman
测试员周周3 天前
【AI测试系统】第6篇:需求扔进去,3 分钟出测试用例?AI测试系统的 RAG 知识增强实战
人工智能·python·功能测试·测试工具·测试用例
测试员周周3 天前
【AI测试功能2】AI功能测试的“不可确定性“难题与应对思路:从精确断言到统计判定的完整方案
大数据·人工智能·python·功能测试·测试工具·单元测试·测试用例