【截图】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")
相关推荐
大汉堡玩测试6 小时前
抓包工具Whistle的安装与使用
测试工具
oi..9 小时前
Linux入门(2)
linux·笔记·测试工具·安全·网络安全
路飞雪吖~10 小时前
【测试】接口测试---1个框架,5个模块
开发语言·python·测试工具
独断万古他化10 小时前
基于 Selenium + POM 模式的聊天室系统 UI 自动化测试框架搭建与实践
selenium·测试工具·ui·自动化·测试·pom
Wild_Pointer.1 天前
高效工具实战指南:Beyond Compare文件比较工具
测试工具
Three~stone1 天前
Wireshark 4.6.4 安装教程
网络·测试工具·wireshark
念越1 天前
蓝桥杯自动化测试模拟1期答案及代码解析(完整版)
自动化测试·软件测试·selenium·蓝桥杯
Luminbox紫创测控2 天前
太阳光模拟器在汽车智能玻璃的运用
测试工具·汽车
西瓜程序猿2 天前
如何将纸质版礼薄转换为电子礼薄?可以使用记了么
测试工具·创业创新
123过去2 天前
crunch使用教程
linux·测试工具·安全