【截图】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")
相关推荐
2601_962295586 小时前
python+selenium实现自动化测试
自动化测试·python·selenium·学习心得·web端测试
测试199815 小时前
Jmeter接口自动化生成测试报告html格式详解
自动化测试·python·测试工具·jmeter·职场和发展·测试用例·接口测试
大汉堡玩测试1 天前
langfuse测试实战(一): 配置一个简单的项目快速落地
python·测试工具
B2_Proxy2 天前
Selenium配置代理IP的完整指南:从启动参数到认证扩展
selenium·测试工具
weixin_440730502 天前
Playwright介绍、特点、小例子还有与传统selenium的区别
selenium·测试工具·playwright
chuntian_tester2 天前
测试中的提示词工程
人工智能·测试工具·ai
测试狗科研平台2 天前
电池材料表征、电芯制备与性能测试--测试狗先进能源中试服务
科技·测试工具·材料工程
啊阿狸不会拉杆3 天前
《计算机网络-自顶向下方法》2.8 小结、课后习题和 Wireshark 实验 读书笔记
计算机网络·测试工具·wireshark·因特网
PS测3 天前
Linux压力测试工具stress,能精确控制 CPU、内存、IO 负载类型和持续时间
测试工具