【截图】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")
相关推荐
PS测3 小时前
Linux压力测试工具stress,能精确控制 CPU、内存、IO 负载类型和持续时间
测试工具
2601_962387827 小时前
web自动化测试实战教程【selenium/unittest/pytest】【共193课
selenium·pytest·devops·web自动化测试·unittest
qianshi121310 小时前
涂层划痕测试仪-精准评估涂层附着力,兼顾科研与工业质量检测
经验分享·测试工具·百度
PhotonixBay10 小时前
3D共聚焦显微镜与探针式粗糙度仪:谁更适合亚表面损伤检测?
图像处理·人工智能·测试工具·3d
2601_9622972510 小时前
Python、Pytest、Allure、Selenium和Jenkins实现自动化测试集成实例
python·selenium·jenkins·pytest·allure
qianshi121310 小时前
电化学腐蚀摩擦磨损试验机的工作原理、操作流程、适用范围行业和产品特点
测试工具·百度
是吕先森12 小时前
【python】selenium实现web自动化测试
前端·python·selenium
Luminbox紫创测控12 小时前
车载AR-HUD阳光倒灌与热效应:太阳光模拟器如何复现太阳辐射?
测试工具·汽车·ar·安全性测试·测试标准
xieliyu.13 小时前
计算机网络:Fiddler 抓包工具使用教程 + HTTP 协议报文格式详解
java·笔记·计算机网络·测试工具·http·java-ee·fiddler