【截图】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")
相关推荐
123过去1 小时前
trufflehog使用教程
linux·测试工具·安全
禾小西1 小时前
性能测试后的瓶颈定位与调优:自下而上找问题,自上而下解难题
java·测试工具
测试19985 小时前
python+selenium 定位到元素,无法点击的解决方法
自动化测试·软件测试·python·selenium·测试工具·测试用例·压力测试
shughui5 小时前
Fiddler(二):自动转发(AutoResponder)功能详解
前端·测试工具·fiddler
程序员三藏5 小时前
接口自动化测试思路和实战:编写线性测试脚本实战
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试
程序员杰哥6 小时前
软件测试之黑盒测试详解
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
程序员杰哥7 小时前
Web UI自动化测试之PO篇
自动化测试·软件测试·python·selenium·测试工具·ui·测试用例
123过去7 小时前
sucrack使用教程
linux·网络·测试工具·安全
天才测试猿7 小时前
Python接口自动化测试之Token详解及应用
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试