【截图】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")
相关推荐
杭州双色云谱8 小时前
2026年蓝光光谱照度计选购与实战指南:双色云谱如何守护光环境安全?
科技·测试工具
一孤程11 小时前
AI辅助测试用例生成实战——用大模型把测试设计效率提升300%
人工智能·测试工具·ai·aigc·测试用例·测试·测试覆盖率
测试员周周1 天前
【skills5】一份 spec 生成 k6/JMeter/Locust:性能压测 + 全站截图,上线前的双保险
功能测试·网络协议·测试工具·jmeter·http·自动化·集成测试
蝶恋舞者1 天前
Wireshark 抓包工具
网络·测试工具·wireshark
天才测试猿1 天前
如何封装自动化测试框架?
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
REST_30272 天前
从Excel日报到实时看板:2026年使用制造工序状态同步工具的半年复盘
数据库·人工智能·测试工具·知识图谱·制造
胡渠洋2 天前
postman学习
学习·测试工具·postman
ClouGence2 天前
8 款好用的 Web 自动化测试工具大盘点:怎么选、适合谁?
selenium·测试
xy34533 天前
wireshark 如何捕获信息
网络·测试工具·渗透测试·wireshark