【截图】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")
相关推荐
梧桐16811 小时前
14天、31个缺陷、28个测试脚本—用 Claude Code 完成第三方软件测试
人工智能·测试工具
mewowaBc19 小时前
postman使用细则
测试工具·postman·偏差
光影6271 天前
Unittest单元测试基础速记
测试工具·单元测试·json·测试覆盖率
xie881018022 天前
正则里的点号不匹配换行:多行模式的开关
运维·服务器·测试工具
2601_967987373 天前
Python在自动化测试中有哪些应用?
python自动化测试·selenium·appium·pytest·性能测试
东舟技术3 天前
从人工目检到AI视觉:智能座舱瞬态故障的自动化捕获与溯源
功能测试·测试工具·自动化
小猿A4 天前
【2026最新】Apifox下载、配置、实战教程(附官网安装包)
测试工具
泽众云测试4 天前
泽众软件与广州华南检验检测中心签约,PerformanceRunner性能测试工具助力第三方测评机构性能检测能力升级
软件测试·测试工具·性能测试
@zulnger5 天前
Charles抓包工具
测试工具
ck0135795 天前
视频添加水印的实践:文字叠加、透明 PNG 与时间区间设置
图像处理·测试工具·音视频·视频