【截图】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")
相关推荐
我的xiaodoujiao8 小时前
API 接口自动化测试详细图文教程学习系列32--Allure测试报告2
python·学习·测试工具·pytest
ClouGence15 小时前
哪些场景适合做自动化测试?这 4 类最值得优先投入
selenium·测试
测试老哥17 小时前
接口自动化测试分层设计与实践总结
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试
ClouGence18 小时前
Selenium vs CueCast:自动化测试一定要写代码吗?
selenium·测试
goodesocket19 小时前
市面上测试稳定可靠的内存条测试治具公司芯片检测利器
测试工具
可可南木2 天前
3070文件格式--23--fixture文件 5
功能测试·测试工具·pcb工艺
小白上线*^_^*2 天前
Jmeter从入门到精通指南
软件测试·测试工具·jmeter·接口测试·测试工程师
goodesocket2 天前
手机LPDDR芯片测试座厂商适配性强
测试工具