【截图】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")
相关推荐
weixin_307779133 小时前
智能模拟数据生成平台:生成式AI合成数据技术重塑开发测试效能
人工智能·测试工具·算法·测试用例
Lust Dusk7 小时前
postman工具介绍(附下载教程)
测试工具·安全·web安全·postman
忧云8 小时前
Wireshark 中文整完整安装与实操教程
测试工具·wireshark·抓包工具·tcp抓包
yly2015020310 小时前
wrk 使用教程
测试工具
我的xiaodoujiao1 天前
API 接口自动化测试详细图文教程学习系列25--继续处理testCase中的数据
python·学习·测试工具·pytest
сокол1 天前
【网安-研判-WireShark流量分析】网页、RAT、FTP、垃圾邮箱的流量过滤规则实战
网络·测试工具·wireshark
川石课堂软件测试1 天前
UI自动化测试|XPath元素定位实践
功能测试·测试工具·jmeter·microsoft·ui·postman·harmonyos
IT界的老黄牛1 天前
手机 Chrome 远程调试实战:adb + DevTools,localhost 就是你的测试服
chrome·测试工具·adb
сокол1 天前
【网安-研判-WireShark流量分析】HTTPS流量解密
测试工具·https·wireshark