【截图】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")
相关推荐
Kingairy3 小时前
PageSpeed Insights,Lighthouse与WebPageTest
测试工具
在坚持一下我可没意见5 小时前
ideaPool论坛系统测试报告
java·spring boot·功能测试·selenium·jmeter·mybatis·压力测试
MarkHD5 小时前
Selenium环境搭建完全指南:WebDriver版本匹配与生产级配置实践
selenium·测试工具
MarkHD5 小时前
Selenium环境搭建完全指南:WebDriver版本匹配与生产级配置实践(Day 21-23)
selenium·测试工具
0思必得019 小时前
[Web自动化] Selenium无头模式
前端·爬虫·selenium·自动化·web自动化
薯条不要番茄酱1 天前
【测试实战篇】“发好论坛”接口自动化测试
python·功能测试·测试工具·单元测试·测试用例·pytest·测试覆盖率
weixin_419658311 天前
当Pytest遇见AI:基于Trae的接口测试用例全自动生成实践
人工智能·python·测试工具·测试用例·pytest·ai编程
hartyu1 天前
纯PHP + Selenium + ChromeDriver方案实现原理,半自动化内容抓取
开发语言·selenium·php
0思必得01 天前
[Web自动化] Selenium日期控件处理
前端·selenium·自动化·web自动化
佚泽2 天前
Java selenium 基本使用
java·开发语言·selenium