【截图】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")
相关推荐
非凡ghost1 小时前
Wireshark中文版(网络抓包工具)
网络·windows·学习·测试工具·wireshark·软件需求
可可南木13 小时前
3070文件格式--5--board文件格式详解 5
功能测试·测试工具·pcb工艺
测试老哥14 小时前
接口测试:加密和签名
自动化测试·软件测试·python·功能测试·测试工具·测试用例·接口测试
xuyuan199815 小时前
超越Selenium:自动化测试框架Cypress在现代前端测试中的卓越实践(windows版本)三
前端·windows·测试工具·系统架构·cypress
天才测试猿15 小时前
自动化测试用例编写
自动化测试·软件测试·python·测试工具·程序人生·职场和发展·测试用例
测试19981 天前
用Postman测WebSocket接口
自动化测试·软件测试·python·websocket·测试工具·接口测试·postman
Wpa.wk2 天前
性能测试-性能监控相关命令-基础篇
android·linux·运维·经验分享·测试工具·性能测试·性能监控
半路_出家ren2 天前
20.基于Selenium实现界面自动化控制
运维·python·selenium·测试工具·网络安全·自动化·chromedriver
_OP_CHEN2 天前
【测试理论与实践】(十)Web 项目自动化测试实战:从 0 到 1 搭建博客系统 UI 自动化框架
运维·自动化测试·python·测试开发·selenium·自动化·测试开发工程师