【截图】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")
相关推荐
St_rive1 小时前
selenium cookie的处理
数据库·selenium·测试工具
随风而飘1862 小时前
安立(Anritsu)MT8820C无线综合测试仪(手机综测仪)
网络·功能测试·测试工具·5g·智能手机
m24tyy17 小时前
常见空间测绘搜索引擎介绍和使用以及子域名的收集
网络·测试工具·搜索引擎
程序员三藏21 小时前
浅谈性能测试
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·性能测试
是Leo刘啊1 天前
别再搜了:15 张速查表,覆盖日常高频查询
测试工具
上海控安1 天前
实时系统保障关键技术-最坏情况执行时间(WCET)分析
测试工具·安全
程序员三藏2 天前
Python+requests实现接口自动化测试
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试
St_rive2 天前
Selenium Grid
selenium·测试工具
qq_513728042 天前
浏览器配置 + 文件上传 + 截图
python·selenium·测试工具·计算机外设