• 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']
    
  • 相关阅读:
    Access更新数据
    linux如何修改主机名
    实习一个月
    网络游戏开发过程(转)
    实习
    不可征服曼德拉
    作为一个河南人
    屌丝男关于游戏市场的一些想法
    实习2周
    一个C/S结构的优秀例子: 延迟补偿在C/S架构游戏协议设计和优化中的应用
  • 原文地址:https://www.cnblogs.com/snailrunning/p/12199401.html
Copyright © 2020-2023  润新知