今天讲什么
本来不太想在更新selenium的相关知识了,因为...没人看!那今天就再更新一篇吧。关于selenium截图的骚操作...
网页屏幕截图
有时候,有时候,你会相信一切有尽头...当你的代码走到了尽头,那么保留最后一刻的状态尤为重要,此时你该如何操作?记录日志...没有将浏览器当前的状态进行截图来的直观!
那么,selenium截取截屏,有哪些方法呢?有四个。
说到讲方法,什么都没有直接看源码来的直观,我带大家走一波,跟进别迷路!
- 先来看看 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了另外一个方法,接着走...
- 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
哥们也是好心整理,但以后还是看我的帖子吧,不迷路....我说了有四个,这才两个啊,还剩下俩呢?
- 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解码得到了二进制数据。接着走
- 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内容就更新到这里