【截图】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")
相关推荐
逸雨清风16 小时前
Chrome更新到136以后selenium等自动化浏览器失效
selenium·自动化
小白学大数据17 小时前
Python+Selenium爬虫:豆瓣登录反反爬策略解析
分布式·爬虫·python·selenium
小冯的编程学习之路1 天前
【软件测试】:推荐一些接口与自动化测试学习练习网站(API测试与自动化学习全攻略)
c++·selenium·测试工具·jmeter·自动化·测试用例·postman
Ten peaches1 天前
Selenium-Java版(操作元素)
java·selenium·测试工具·html
傻小胖1 天前
三种常见接口测试工具(Apipost、Apifox、Postman)
测试工具·postman
辛普森Mmmm2 天前
Postman接口测试
测试工具·postman
程序员三藏2 天前
软件测试之功能测试详解
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
Looooking2 天前
Python 之 selenium 打开浏览器指定端口进行接续操作
python·selenium
头疼的程序员2 天前
allure生成测试报告(搭配Pytest、allure-pytest)
测试工具·pytest
程序员杰哥3 天前
自动化测试基础知识详解
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例