【截图】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")
相关推荐
小白学大数据21 小时前
AI 智能爬虫实战:Selenium+Python 自动绕反爬、一键提取数据
爬虫·python·selenium·数据分析
君科程序定做21 小时前
基于 Codex + Selenium 的 CNKI 博士论文开题调研自动化流程
selenium·测试工具·自动化
废弃的小码农1 天前
APP测试--adb使用介绍
python·测试工具·adb
三千花灯1 天前
【Playwright】安装
人工智能·测试工具
@zulnger1 天前
自动化测试框架:Selenium 剖析(1.2)
selenium·测试工具
程序员小远1 天前
Python自动化测试框架及工具详解
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试
Land03292 天前
RPA网页自动化:元素定位失效的7种根因与XPath鲁棒性改造方案
运维·selenium·自动化·rpa
上天_去_做颗惺星 EVE_BLUE2 天前
Ubuntu Android 虚拟机安装使用教程
android·linux·测试工具·ubuntu·安卓
测试老哥2 天前
接口测试详解
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试