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']
    
  • 相关阅读:
    关于Python的super用法研究
    服务器提示缺少JAVA_HOME
    python_popen
    nslookup
    day2--深copy和浅copy
    if/while/fore根据编号购买商品
    标志位的用法
    python 循环和file操作实现用户密码输错三次将用户锁定
    day01项目:用户三次登陆锁定
    day01——python从认识开始
  • 原文地址:https://www.cnblogs.com/snailrunning/p/12199401.html
Copyright © 2011-2022 走看看