【截图】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")
相关推荐
线束线缆组件品替网2 小时前
Finisar AOC/DAC 在高速网络互连中的应用解析
网络·数码相机·测试工具·电脑·pcb工艺·游戏机
测试19982 小时前
如何使用Appium实现移动端UI自动化测试?
自动化测试·软件测试·python·测试工具·ui·appium·测试用例
though the night7 小时前
Python UI 自动化测试框架搭建demo(Selenium+Pytest 版)
自动化测试·selenium·测试工具·pytest
Wpa.wk7 小时前
Docker原理和使用场景(网络模式和分布式UI自动化环境部署)
linux·经验分享·分布式·测试工具·docker·性能监控
我送炭你添花8 小时前
软件测试为何不可或缺?——以复杂宏系统与 PTZ 控制为例,深度解析 pytest 的实战价值与不可替代性
python·测试工具·pytest
深蓝电商API10 小时前
Selenium 截图与元素高亮定位技巧
爬虫·python·selenium
我的xiaodoujiao1 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 43--添加allure测试报告显示信息和其他封装方法
python·学习·测试工具·allure
夜郎king1 天前
Java搭配Selenium:实现网页访问与自动截图的实战指南及常见问题解析
selenium·java 网页截图·selenium截图实现
深蓝电商API1 天前
Selenium 爬取 Canvas 渲染的数据图表
爬虫·python·selenium