【截图】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")
相关推荐
Luminbox紫创测控2 小时前
大面积太阳模拟器设计原理与高均匀度辐照实现方法
人工智能·测试工具·安全性测试·测试标准
m0_752035633 小时前
postman重新安装后历史接口数据不见了
测试工具·postman
程序员小远6 小时前
性能测试之性能调优
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·性能测试
xy345316 小时前
Wireshark捕获过滤器——语法元素详解
网络·测试工具·渗透测试·wireshark
PhotonixBay1 天前
共聚焦成像核心原理:针孔、PSF与三维形貌测量技术
人工智能·测试工具·算法
程序员小远1 天前
接口测试之文件上传
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试
kft13141 天前
Playwright + 本地 AI,从录制到自愈闭环的一站式自动化测试平台。
人工智能·测试工具
ClouGence1 天前
零代码自动化测试工具回演 CueCast 新版本发布:数据变量、多标签页录制与登录准备能力上线
selenium·测试