【截图】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")
相关推荐
xie881018022 小时前
长表格跨页重复表头:排版上的做法
测试工具
老孟说禅8 小时前
测试工具示波器的使用
测试工具
步行cgn1 天前
为什么使用接口测试工具?
测试工具
P1Browser1 天前
Puppeteer 与 Selenium 技术对比:多账号自动化管理如何做环境隔离?
selenium·测试工具·自动化
weihubeats2 天前
10 MB 的 Postman 替代品,启动不到 1 秒
测试工具·postman
Meta Luo5 天前
UI 自动化测试中的等待、超时和异步加载怎么处理
功能测试·测试工具·ui·回归·自动化
Data_Journal5 天前
如何使用 Selenium 和 Node.js 绕过 CAPTCHA
selenium
yeflx5 天前
Ubuntu22.04下通过Wireshark抓取相机IP
tcp/ip·测试工具·wireshark
总线通信小百科5 天前
100BASE-T1媒体转换器选型:接口、主从模式与供电怎么判断?
经验分享·测试工具
郝学胜-神的一滴5 天前
Python浏览器自动化:Selenium与Playwright实战对比,网页爬虫&自动操作一站式指南
开发语言·爬虫·python·selenium·程序人生·自动化