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']
    
  • 相关阅读:
    Stochastic Gradient Descent 随机梯度下降法-R实现
    Gradient Descent 梯度下降法-R实现
    【转】Linux内核中分配4M以上大内存的方法
    【转】在Linux下写一个简单的驱动程序
    【转】内核中的内存申请:kmalloc、vmalloc、kzalloc、kcalloc、get_free_pages
    【转】uboot中的mmc命令
    【原】cmdline传递参数 uboot-->kernel-->fs
    【转】UBOOT——启动内核
    【转】Linux下的磁盘分区方法
    【转】linux下 如何切换到root用户
  • 原文地址:https://www.cnblogs.com/snailrunning/p/12199401.html
Copyright © 2011-2022 走看看