【截图】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")
相关推荐
测试19981 小时前
从登录测试谈测试用例
自动化测试·软件测试·功能测试·测试工具·测试用例·性能测试·安全性测试
上天_去_做颗惺星 EVE_BLUE6 小时前
Linux Core Dump 测试操作手册
linux·c++·测试工具
吉哥机顶盒刷机7 小时前
晶晨烧录软件无法安装驱动解决办法
测试工具·晶晨烧录工具
123过去7 小时前
mfterm使用教程
linux·网络·测试工具·安全
123过去7 小时前
nfc-mfclassic使用教程
linux·网络·测试工具·安全
Wpa.wk20 小时前
APP测试 - 压力测试与性能分析3
测试工具·自动化·压力测试
大连好光景1 天前
软件测试笔记(1)
笔记·功能测试·测试工具
123过去1 天前
responder使用教程
linux·网络·测试工具·安全·哈希算法
我的xiaodoujiao1 天前
API 接口自动化测试详细图文教程学习系列7--相关Python基础知识6
python·学习·测试工具·pytest
愤豆1 天前
11-Java语言核心-JVM原理-JVM调优详解
java·jvm·测试工具