zoukankan      html  css  js  c++  java
  • Selenium(六):截图

    截图

    from selenium import webdriver
    driver = webdriver.Chrome()
    # 以PNG格式,保存浏览器截图,filename为截图文件绝对路径
    driver.get_screenshot_as_file()
    
    # 以PNG格式,保存浏览器截图,filename为截图文件绝对路径
    driver.save_screenshot()
    # 上述两个方法等价
    

    /site-packages/selenium/webdriver/remote/webdriver.py

    class WebDriver(object):
    
       def get_screenshot_as_file(self, filename):
            """
            Saves a screenshot of the current window to a PNG image file. Returns
               False if there is any IOError, else returns True. Use full paths in
               your filename.
    
            :Args:
             - filename: The full path you wish to save your screenshot to. This
               should end with a `.png` extension.
    
            :Usage:
                driver.get_screenshot_as_file('/Screenshots/foo.png')
            """
            if not filename.lower().endswith('.png'):
                warnings.warn("name used for saved screenshot does not match file "
                              "type. It should end with a `.png` extension", UserWarning)
            png = self.get_screenshot_as_png()
            try:
                with open(filename, 'wb') as f:
                    f.write(png)
            except IOError:
                return False
            finally:
                del png
            return True
    
        def save_screenshot(self, filename):
            """
            Saves a screenshot of the current window to a PNG image file. Returns
               False if there is any IOError, else returns True. Use full paths in
               your filename.
    
            :Args:
             - filename: The full path you wish to save your screenshot to. This
               should end with a `.png` extension.
    
            :Usage:
                driver.save_screenshot('/Screenshots/foo.png')
            """
            return self.get_screenshot_as_file(filename)
    
        def get_screenshot_as_png(self):
            """
            Gets the screenshot of the current window as a binary data.
    
            :Usage:
                driver.get_screenshot_as_png()
            """
            return base64.b64decode(self.get_screenshot_as_base64().encode('ascii'))
    
        def get_screenshot_as_base64(self):
            """
            Gets the screenshot of the current window as a base64 encoded string
               which is useful in embedded images in HTML.
    
            :Usage:
                driver.get_screenshot_as_base64()
            """
            return self.execute(Command.SCREENSHOT)['value']
    
  • 相关阅读:
    [svc]frp内网穿透
    [svc]caffe安装笔记
    [na]icmp重定向
    [tools]python的mkdocs模块分分钟将md搞成一个网站
    [svc]samba服务搭建
    [ci] jenkins的Timestamper插件-让日志显示时间
    [k8s]subpath解决cm覆盖目录问题
    struts2+jquery+ajax实现上传&&校验实例
    java String.split方法是用注意点(转)
    loadrunner java协议脚本要点
  • 原文地址:https://www.cnblogs.com/snailrunning/p/12199401.html
Copyright © 2011-2022 走看看