【截图】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")
相关推荐
princess666 小时前
selenium使用
javascript·selenium·测试工具
程序员小远9 小时前
接口自动化测试框架(pytest+allure+aiohttp+ 用例自动生成)
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
princess6611 小时前
自动化测试工具selenium的安装踩坑
selenium·测试工具·自动化
小码哥说测试13 小时前
pytest-xdist 进行多进程并发测试!
软件测试·网络协议·selenium·单元测试·pytest·压力测试·postman
字节程序员15 小时前
Fiddler抓包工具之fiddler设置过滤
测试工具·fiddler
络71 天前
使用Selenium和Jsoup框架进行Java爬虫
java·爬虫·selenium
安冬的码畜日常2 天前
【玩转 Postman 接口测试与开发2_017】第13章:在 Postman 中实现契约测试(Contract Testing)与 API 接口验证(下)
测试工具·postman·契约测试·contract test·postman契约测试·api契约测试
亿牛云爬虫专家2 天前
FastAPI与Selenium:打造高效的Web数据抓取服务
爬虫·python·selenium·fastapi·图片·代理ip·pixabay
prince_zxill2 天前
使用 Postman 进行 API 测试:从入门到精通
javascript·网络·websocket·测试工具·postman