【截图】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")
相关推荐
se-tester1 天前
JMeter、Postman 和 SoapUI 在做接口测试上的优势和缺点
测试工具·jmeter·接口测试·postman·soapui
xixi09241 天前
selenium IDE——command
selenium·测试工具
xixi09241 天前
selenium IDE安装使用教程
ide·selenium·测试工具
Wpa.wk2 天前
har文件转为接口自动化测试用例
运维·测试工具·自动化·测试用例·接口自动化
Wpa.wk2 天前
ubuntu22桌面版安装
经验分享·测试工具
uyeonashi2 天前
自动化测试问卷考试系统测试报告
功能测试·selenium·github
007张三丰2 天前
软件测试专栏(6/20):Selenium从0到1实战指南:元素定位、等待机制与框架封装
selenium·测试工具
测试老哥2 天前
如何使用Postman做接口测试?
自动化测试·软件测试·python·测试工具·测试用例·接口测试·postman
安全不再安全2 天前
某驱动任意读漏洞分析 - 可用于游戏内存数据读取
c语言·测试工具·安全·游戏·网络安全