【截图】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")
相关推荐
DrMaker11 小时前
【无标题】
软件测试·python·测试工具·pyqt
努力的lpp12 小时前
渗透主流工具完整参数手册(sqlmap、Nmap、Hydra、Dirsearch、Xray)
javascript·网络协议·测试工具·安全·http·工具
小白学大数据1 天前
线上故障急救:依托 OpenClaw 日志排查 403 和 503 问题
爬虫·python·selenium·数据分析
PhotonixBay1 天前
共聚焦显微镜测量铜导体表面粗糙度
测试工具·制造
糖果店的幽灵1 天前
AI 驱动 Selenium 测试框架最佳实践:从传统自动化到智能体测试
人工智能·selenium·自动化
cfm_29141 天前
JVM垃圾收集算法与收集器深度解析
jvm·测试工具·算法·性能优化
Luminbox紫创测控1 天前
金属卤素灯工作原理与汽车零部件老化测试应用
测试工具·汽车·安全性测试·测试标准
Maydaycxc1 天前
Python 实现 RPA + AI 自动化:大模型 OCR + 网页操作完整源码实战
人工智能·python·opencv·selenium·自动化·ocr·rpa
赵大大宝1 天前
Selenium 从入门到精通:自动化测试与爬虫实战全攻略
爬虫·selenium·测试工具
zhonghaoxincekj2 天前
基于 168MHz MCU 的直流继电器全参数自动化测试方案解析
经验分享·功能测试·科技·学习·测试工具·创业创新·制造