大型情感剧集Selenium:8_selenium网页截图的四种方法

简介: 本来不太想在更新selenium的相关知识了,因为...没人看!那今天就再更新一篇吧。关于selenium截图的骚操作...

今天讲什么


本来不太想在更新selenium的相关知识了,因为...没人看!那今天就再更新一篇吧。关于selenium截图的骚操作...


网页屏幕截图


有时候,有时候,你会相信一切有尽头...当你的代码走到了尽头,那么保留最后一刻的状态尤为重要,此时你该如何操作?记录日志...没有将浏览器当前的状态进行截图来的直观!


那么,selenium截取截屏,有哪些方法呢?有四个。

说到讲方法,什么都没有直接看源码来的直观,我带大家走一波,跟进别迷路!


  1. 先来看看 save_screenshot(filename)
"""
    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)

可以看到要求,保存的文件,必须是以==.png==结尾的,朋友们要记得!然后他return了另外一个方法,接着走...


  1. get_screenshot_as_file
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

可以看到,这里有一个文件结尾判断,封装了UserWarning,会给出提示信息并终止程序...


看到这两段代码,你就知道以后yoga那个方法保存文件了吧。肯定用get_screenshot_as_file,干嘛平白无故多一次回调函数...


所以,看源码的好处就是,不至于被某些帖子所误导...如下贴:

网络异常,图片无法展示
|

image.png


哥们也是好心整理,但以后还是看我的帖子吧,不迷路....我说了有四个,这才两个啊,还剩下俩呢?

  1. get_screenshot_as_png()
    看上段代码中的这行png = self.get_screenshot_as_png(),前面俩只是传销最底层发广告的,这是个业务部长...
    你看到先创建,又在finally中 ==del png==,就应该知道,它存中间变量...取方法内部看看

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'))

==binary data.==,嘛意思?二进制数据

怎么来的?刚才说它是业务部长,因为这里还有西北五省总代理,as_png通过b64decode解码得到了二进制数据。接着走


  1. get_screenshot_as_base64()
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']

不知道初高中大家有买过一本辅导书没,叫==各个击破==,学代码应该怎么学,如果你有时间,对模块感兴趣,就该层层剖析的去看!有空大家可以继续往深里面瞅....

看到这里,四个方法看完了...但他们也只是封装调用的,知识点还深着呢。但这四个方法是干嘛的,怎么用,什么关系,至少大家明白了吧!还怕面试的时候问吗?随便来...盘它!


总结一句:

方法 说明
save_screenshot 趁早别用
get_screenshot_as_file 保存网页截图
get_screenshot_as_png 获取二进制数据流
get_screenshot_as_base64 base64编码原始数据


走个例子吧:

from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.jianshu.com/u/d23fd5012bed")
driver.get_screenshot_as_file('BreezePython.png')
driver.close()

网络异常,图片无法展示
|

image.png

是不是so esay,还觉得编程难吗?难...因为这只是最基础的,明天和大家讲如何截取网页中的局部内容,以及selenium网页长图截取,不过,明天好像周五晚上例行休息啊,看明天阅读量了,多就不休了,哈哈...


The End

今天的selenium内容就更新到这里




相关文章
|
8月前
|
存储 搜索推荐 数据挖掘
使用selenium库模拟浏览器行为,获取网页的cookie值
使用selenium库模拟浏览器行为,获取网页的cookie值
|
8月前
|
搜索推荐 API 数据安全/隐私保护
使用Selenium进行网页登录和会话管理
使用Selenium进行网页登录和会话管理
|
5月前
|
移动开发 安全 测试技术
『App自动化测试之Appium应用篇』| 继承于selenium常用的元素定位方法有哪些?如何使用?
『App自动化测试之Appium应用篇』| 继承于selenium常用的元素定位方法有哪些?如何使用?
87 0
|
3月前
|
Linux 索引
Linux中使用selenium截图的文字变为方框的解决方案
Linux中使用selenium截图的文字变为方框的解决方案
38 1
|
4月前
|
Python
Python Appium Selenium 查杀进程的实用方法
Python Appium Selenium 查杀进程的实用方法
37 1
|
9月前
|
XML 数据采集 JSON
scrapy_selenium爬取Ajax、JSON、XML网页:豆瓣电影
在网络爬虫的开发过程中,我们经常会遇到一些动态加载的网页,它们的数据不是直接嵌入在HTML中,而是通过Ajax、JSON、XML等方式异步获取的。这些网页对于传统的scrapy爬虫来说,是很难直接解析的。那么,我们该如何使用scrapy_selenium来爬取这些数据格式的网页呢?本文将为你介绍scrapy_selenium的基本原理和使用方法,并给出一个实际的案例。
|
9月前
selenium--浏览器窗口截图
selenium--浏览器窗口截图
selenium中,切换iframe的方法
selenium中,切换iframe的方法
|
5月前
|
前端开发 测试技术 Python
软件测试/测试开发|Python selenium CSS定位方法详解
软件测试/测试开发|Python selenium CSS定位方法详解
30 0
|
6月前
|
前端开发 测试技术 Python
Python Selenium元素定位方法详解
Python Selenium元素定位方法详解

热门文章

最新文章