【截图】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")
相关推荐
巴里巴气14 小时前
selenium基础知识 和 模拟登录selenium版本
爬虫·python·selenium·爬虫模拟登录
2501_9240641117 小时前
2025年跨端云真机测试平台深度测评:XR与折叠屏时代的兼容性之战
测试工具·移动端自动化测试·自动化测试脚本
Small black human1 天前
HTTP-Postman的安装及其使用
测试工具·postman
AIZHINAN1 天前
Appium 简介
自动化测试·测试工具·appium
吴free1 天前
mac电脑wireshark快速实现http接口抓包
网络·测试工具·http·wireshark
DeamoTech2 天前
ESCADA
物联网·测试工具
旷世奇才李先生2 天前
Selenium 安装使用教程
selenium·测试工具
巴里巴气3 天前
对selenium进行浏览器和驱动进行配置Windows | Linux
selenium·测试工具
q567315233 天前
Java Selenium反爬虫技术方案
java·爬虫·selenium
有趣的我3 天前
wireshark介绍和使用
网络·测试工具·wireshark