【截图】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")
相关推荐
大汉堡玩测试7 小时前
langfuse测试实战(一): 配置一个简单的项目快速落地
python·测试工具
B2_Proxy12 小时前
Selenium配置代理IP的完整指南:从启动参数到认证扩展
selenium·测试工具
weixin_4407305015 小时前
Playwright介绍、特点、小例子还有与传统selenium的区别
selenium·测试工具·playwright
chuntian_tester1 天前
测试中的提示词工程
人工智能·测试工具·ai
测试狗科研平台1 天前
电池材料表征、电芯制备与性能测试--测试狗先进能源中试服务
科技·测试工具·材料工程
啊阿狸不会拉杆2 天前
《计算机网络-自顶向下方法》2.8 小结、课后习题和 Wireshark 实验 读书笔记
计算机网络·测试工具·wireshark·因特网
PS测2 天前
Linux压力测试工具stress,能精确控制 CPU、内存、IO 负载类型和持续时间
测试工具
2601_962387822 天前
web自动化测试实战教程【selenium/unittest/pytest】【共193课
selenium·pytest·devops·web自动化测试·unittest
qianshi12132 天前
涂层划痕测试仪-精准评估涂层附着力,兼顾科研与工业质量检测
经验分享·测试工具·百度
PhotonixBay2 天前
3D共聚焦显微镜与探针式粗糙度仪:谁更适合亚表面损伤检测?
图像处理·人工智能·测试工具·3d