【截图】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")
相关推荐
REDcker1 小时前
Puppeteer 与 Selenium 对比分析
爬虫·selenium·自动化·浏览器·puppeteer
我的xiaodoujiao1 小时前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 40--完善优化 Allure 测试报告显示内容
python·学习·测试工具·pytest
我的xiaodoujiao2 小时前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 41--自定义定制化展示 Allure 测试报告内容
python·学习·测试工具·pytest
玩转数据库管理工具FOR DBLENS4 小时前
人工智能:演进脉络、核心原理与未来之路 审核中
数据库·人工智能·测试工具·数据库开发·数据库架构
小罗和阿泽6 小时前
论坛系统测试报告
功能测试·测试工具·压力测试·可用性测试
写代码的【黑咖啡】18 小时前
Python中的Selenium:强大的浏览器自动化工具
python·selenium·自动化
可可南木21 小时前
3070文件格式--6--board文件格式详解 6
功能测试·测试工具·pcb工艺
程序员雷叔1 天前
在postman设置请求里带动态token,看看这两种方法!
selenium·测试工具·单元测试·测试用例·pytest·lua·postman
Hacker_xingchen1 天前
如何用Postman做接口自动化测试及完美的可视化报告?
自动化测试·软件测试·测试工具·职场和发展·postman
AI软件工程实践1 天前
软件工程里 Postman 的文件上传与下载测试技巧
测试工具·ai·lua·postman