【截图】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")
相关推荐
Wokoo713 小时前
Selenium 核心 API 解析(三)
python·功能测试·selenium
ck01357915 小时前
图片抠图怎么做?从背景去除到透明 PNG 的实用教程
人工智能·测试工具·音视频·媒体·视频
LT101579744418 小时前
2026年国内有哪些好用的性能测试工具?
测试工具
技术人生00120 小时前
Selenium 工程化模板(日志+异常截图,可直接复制使用)
python·selenium·测试工具
Wokoo720 小时前
Selenium:元素定位、动态适配与性能优化(二)
前端·selenium·自动化
ck0135791 天前
视频转 GIF 的基本流程与文件大小优化
前端·测试工具·html·音视频·视频
菜鸟阿达2 天前
Postman 有哪些免费/开源的替代品?2026 年接口工具选型参考
测试工具·开源·postman
代码不停2 天前
自动化测试(java)
java·开发语言·selenium·自动化
雪雪爱冲浪3 天前
2026 年网络爬虫领域的Puppeteer vs. Playwright
爬虫·网络协议·测试工具·接口隔离原则