【截图】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")
相关推荐
我的xiaodoujiao3 小时前
使用PyMySQL模块技术操作MySQL数据库
数据库·python·测试工具·mysql
摸摸芋21 小时前
postman(ai篇)
测试工具·postman
St_rive1 天前
selenium元素定位方法
selenium·测试工具
St_rive1 天前
selenium常用等待方式
selenium·测试工具
随风而飘1861 天前
罗德与施瓦茨 FSP13 中高端频谱分析仪
网络·功能测试·测试工具
测试狗科研平台1 天前
从实验室到量产:测试狗新材料中试的技术路径与平台支撑
测试工具·服务发现·材料工程
szarron1 天前
VNA6 便携式矢量网络分析仪:从入门到精通
数据库·嵌入式硬件·测试工具·5g·射频工程
不拘一格的叶三少1 天前
Pytest + Playwright 自动化测试工程设计要点 & 目录结构
测试工具·pytest
测试19983 天前
Python接口测试之requests库安装和导入
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试