【截图】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")
相关推荐
transitory_truth6 分钟前
POSTMAN使用
测试工具·postman
Wpa.wk2 小时前
接口自动化 - 解决大量响应数据字段的格式断言 -Json-schema
运维·经验分享·测试工具·自动化·json·接口测试
Wpa.wk5 小时前
接口自动化 - 接口组合业务练习(CRUD组合)-REST-assure(Java版)
java·运维·经验分享·测试工具·自动化·接口自动化
天才测试猿20 小时前
Selenium测试框架快速搭建详解
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
Wpa.wk1 天前
接口测试-多层嵌套响应处理-JSONPath使用(Java版)
java·前端·经验分享·python·测试工具·jsonpath
yenggd2 天前
wireshark常规用法
测试工具·wireshark·php
我送炭你添花2 天前
Pelco KBD300A 模拟器:06+3.从教学级到企业级工程化转型(二次迭代)
python·测试工具·运维开发
会打莎士比亚的猴子2 天前
tcpdump移植
网络·测试工具·tcpdump
KevinGuo4572 天前
Selenium3自动化测试实战——基于python语言
开发语言·python·selenium
white-persist2 天前
【内网运维】Netstat与Wireshark:内网运维溯源实战解析
运维·网络·数据结构·测试工具·算法·网络安全·wireshark