【截图】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")
相关推荐
flower_drop1 小时前
基于 WebUSB 与 CDP:在浏览器端实现 Android 设备通信与无证书抓包实践
android·chrome·测试工具·adb·bug·edge浏览器
2601_962298276 小时前
Python与Selenium结合的Web自动化测试全流程实践教程
自动化测试·python·selenium·web测试·pageobjectmodel
chuntian_tester9 小时前
AI自动化第3步【用例设计】
人工智能·测试工具·ai·自动化
广州文明机电有限公司10 小时前
LM‑2 实操|宽域氧传感器安装位置 + Free‑Air 自由空气校准完整流程
功能测试·测试工具
PhotonixBay10 小时前
共聚焦显微镜如何实现表面形貌测量
图像处理·测试工具·共聚焦显微镜·表面粗糙度·激光共聚焦显微镜·表面形貌
chuntian_tester11 小时前
AI自动化第1步【系统探索】
人工智能·测试工具·ai·自动化
2601_9622982713 小时前
【自动化测试】基于Selenium + Python的web自动化框架
自动化测试·python·selenium·测试用例·web框架
2601_962295581 天前
python+selenium实现自动化测试
自动化测试·python·selenium·学习心得·web端测试
测试19982 天前
Jmeter接口自动化生成测试报告html格式详解
自动化测试·python·测试工具·jmeter·职场和发展·测试用例·接口测试