【截图】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 小时前
postman学习
学习·测试工具·postman
ClouGence9 小时前
8 款好用的 Web 自动化测试工具大盘点:怎么选、适合谁?
selenium·测试
xy34531 天前
wireshark 如何捕获信息
网络·测试工具·渗透测试·wireshark
会周易的程序员1 天前
从零构建多核CPU负载自适应控制系统
linux·c++·笔记·物联网·测试工具
博观而约取厚积而薄发1 天前
Pytest 从入门到精通,一篇就够(超详细实战教程)
python·测试工具·单元测试·自动化·pytest
hbugs0012 天前
BPF 表达式检查器:一款免费开源的 Wireshark 捕获过滤器语法检查与可视化构建工具
网络·测试工具·开源·eve-ng·bpf
测试秃头怪3 天前
如何评估自动化测试脚本的编写时间和维护工作量?
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
程序员杰哥3 天前
接口测试知识总结
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试
尼古拉斯·纯情暖男·天真·阿玮3 天前
正交表的概念与生成
测试工具