【截图】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")
相关推荐
wuchen10041 天前
使用Postman测试grpc接口-传参错误的反例
测试工具·postman
蒲公英内测分发1 天前
Typeoff 实时润色体验:语音转文字让写作效率提升 3 倍
测试工具·产品运营·项目管理
Luminbox紫创测控1 天前
氙灯太阳光模拟器加速老化测试
人工智能·测试工具·测试标准
wangl_922 天前
Wireshark 使用指南:从入门到高级分析
网络·网络协议·tcp/ip·测试工具·wireshark·modbus
Byron Loong2 天前
【网络】Wireshark过滤器表达式的规则
网络·测试工具·wireshark
MESMarketing2 天前
互动分享 | Shift-Left实践落地
功能测试·测试工具·自动化·自动驾驶·敏捷开发
lifewange2 天前
主流性能诊断工具
测试工具
程序员杰哥2 天前
独立搭建UI自动化测试框架
自动化测试·软件测试·python·selenium·测试工具·ui·测试用例
PhotonixBay3 天前
激光共聚焦显微镜如何实现CVD石墨烯实时质量控制
人工智能·测试工具
zhonghaoxincekj4 天前
轴距可调式元器件双边无损成形钳
经验分享·科技·深度学习·学习·测试工具·创业创新·制造