【截图】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")
相关推荐
德思特11 小时前
通过 Wireshark 抓取串口命令
网络协议·测试工具·wireshark
影sir15 小时前
Selenium常用函数(窗口与弹窗)
selenium·测试工具
Luminbox紫创测控1 天前
基于环境舱的新能源汽车三高试验方法与热响应评估
大数据·人工智能·测试工具·汽车·安全性测试·测试标准
菠萝猫yena2 天前
【读书笔记】《测试架构师修炼之道》读书笔记
功能测试·测试工具·单元测试
PhotonixBay2 天前
金属增材制造表面测量:共聚焦显微镜参数优化实践
人工智能·测试工具·制造
LT10157974442 天前
2026年 AI+RPA平台选型指南:智能自动化高效落地
测试工具
写出高质量的博客2 天前
Selenium常用方法
selenium·测试工具
程序员杰哥2 天前
Python+requests+excel 接口自动化测试框架
自动化测试·软件测试·python·测试工具·测试用例·excel·接口测试
介一安全2 天前
【Web安全】JWT常见安全漏洞总结
测试工具·安全·web安全·安全性测试