【截图】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")
相关推荐
阿斯加德D10 小时前
我的世界生活大冒险整合包下载高版本2026最新分享
测试工具·游戏·游戏程序·生活·材质
Land032911 小时前
指纹浏览器自动化集成方案|多浏览器RPA适配实战记录
运维·人工智能·爬虫·python·selenium·自动化·rpa
littlebigbar12 小时前
让AI自己说说,AI 智能体在软件测试中能做什么?
人工智能·测试工具
测试员周周12 小时前
【Appium 系列】第07节-API测试封装 — BaseAPI 的设计与实现
开发语言·人工智能·功能测试·测试工具·appium·自动化·测试用例
yuanpan13 小时前
Python + Selenium 浏览器自动化测试与网页自动登录
开发语言·python·selenium
骄傲的心别枯萎16 小时前
WireShark抓取rtsp包
网络·测试工具·wireshark
Luminbox紫创测控17 小时前
汽车(EV)内外饰材料老化测试与标准
人工智能·测试工具·汽车·安全性测试·测试标准
光影6271 天前
Selenium自动化测试---实战踩坑实录
python·selenium·测试工具·百度
掌动智能1 天前
传统数据工厂之死:RunnerAgent如何开启AI驱动的“数据生产”新纪元
人工智能·测试工具·自动化
wuchen10042 天前
使用Postman测试grpc接口-传参错误的反例
测试工具·postman