【截图】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")
相关推荐
Wpa.wk2 小时前
Docker容器 - 了解Docker基础命令(容器+镜像命令)
运维·经验分享·测试工具·docker·容器
测试19984 小时前
2026最新软件测试面试八股文(含答案+文档)
自动化测试·软件测试·功能测试·测试工具·面试·职场和发展·测试用例
zzZ··*5 小时前
自动登录上海大学校园
python·网络协议·selenium
西安同步高经理5 小时前
日差检定测试仪的功能作用与使用方法详解、日差测试仪、日差测量
测试工具
深蓝电商API6 小时前
Selenium处理iframe嵌套页面实战
爬虫·python·selenium
MESMarketing7 小时前
互动分享 | ISO 26262安全分析概览
功能测试·测试工具·安全·网络安全·matlab
卓码软件测评18 小时前
第三方软件测试测评机构【使用web_reg_save_param_ex函数:掌握LoadRunner关联的黄金法则 】
测试工具·ci/cd·性能优化·单元测试·测试用例
大飞记Python1 天前
代码级教程|用Playwright实现Web自动化测试:从零到录制生成脚本
自动化测试·python·selenium·playwright
weixin_438732101 天前
ChromeDriver谷歌驱动下载
linux·chrome·selenium·自动化·mac·chrome devtools·chromedriver