【截图】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")
相关推荐
测试19986 小时前
软件测试 - 单元测试总结
自动化测试·软件测试·python·测试工具·职场和发展·单元测试·测试用例
影sir9 小时前
Selenium常用函数(等待)
selenium·测试工具
影sir9 小时前
Selenium常用函数(浏览器导航,文件上传,浏览器参数设置)
selenium·测试工具
WL_Aurora1 天前
Python爬虫实战(七):Selenium自动化采集苏宁易购商品数据
爬虫·python·selenium
德思特2 天前
通过 Wireshark 抓取串口命令
网络协议·测试工具·wireshark
影sir2 天前
Selenium常用函数(窗口与弹窗)
selenium·测试工具
Luminbox紫创测控2 天前
基于环境舱的新能源汽车三高试验方法与热响应评估
大数据·人工智能·测试工具·汽车·安全性测试·测试标准
菠萝猫yena3 天前
【读书笔记】《测试架构师修炼之道》读书笔记
功能测试·测试工具·单元测试
PhotonixBay3 天前
金属增材制造表面测量:共聚焦显微镜参数优化实践
人工智能·测试工具·制造