python自动化测试selenium屏幕截图示例
作者:aovenus 时间:2022-07-13 16:55:14
WebDriver内置了测试中捕获屏幕并保存的方法。
示例脚本:
(1)save_screenshot(filename):保存屏幕截图
from selenium import webdriver
from time import sleep,strftime,localtime,time
import os
class TestScreenShot(object):
def setup(self):
self.driver = webdriver.Chrome()
self.driver.get("http://www.baidu.com")
def test_screen(self):
self.driver.save_screenshot("testbaidu.png")
def teardown(self):
self.driver.quit()
if __name__ == '__main__':
shot=TestScreenShot()
shot.test_screen()
(2)get_screenshot_as_base64():获取当前屏幕截图base64编码字符串
from selenium import webdriver
from time import sleep,strftime,localtime,time
import os
class TestScreenShot(object):
def setup(self):
self.driver = webdriver.Chrome()
self.driver.get("http://www.baidu.com")
def test_screen(self):
self.driver.save_screenshot("testbaidu.png")
print(self.driver.get_screenshot_as_base64())
def teardown(self):
self.driver.quit()
if __name__ == '__main__':
shot=TestScreenShot()
shot.test_screen()
运行结果:
(3)get_screenshot_as_file(filename):获取当前屏幕截图,使用完整路径
from selenium import webdriver
from time import sleep,strftime,localtime,time
import os
class TestScreenShot(object):
def setup(self):
self.driver = webdriver.Chrome()
self.driver.get("http://www.baidu.com")
def test_screen(self):
self.driver.get_screenshot_as_file("testbaidu2.png")
def teardown(self):
self.driver.quit()
if __name__ == '__main__':
shot=TestScreenShot()
shot.test_screen()
(4)get_screenshot_as_file(filename):获取当前屏幕截图,使用完整路径,文件名以时间命名
from selenium import webdriver
from time import sleep,strftime,localtime,time
import os
class TestScreenShot(object):
def setup(self):
self.driver = webdriver.Chrome()
self.driver.get("http://www.baidu.com")
def test_screen(self):
# print(self.driver.get_screenshot_as_png())
# self.driver.get_screenshot_as_file("testbaidu2.png")
# self.driver.save_screenshot("testbaidu.png")
# print(self.driver.get_screenshot_as_base64())
self.driver.find_element_by_id("kw").send_keys("selenium test")
self.driver.find_element_by_id("su").click()
str = strftime("%Y-%m-%d-%H-%M-%S",localtime(time()))
filename = str + ".png"
path =os.path.abspath("screenshots")
filepath=path + '/'+filename
self.driver.get_screenshot_as_file(filepath)
print(filepath)
def teardown(self):
self.driver.quit()
if __name__ == '__main__':
shot=TestScreenShot()
shot.test_screen()
(5)get_screenshot_as_png():获取当前屏幕截图的二进制文件数据
from selenium import webdriver
from time import sleep,strftime,localtime,time
import os
class TestScreenShot(object):
def setup(self):
self.driver = webdriver.Chrome()
self.driver.get("http://www.baidu.com")
def test_screen(self):
print(self.driver.get_screenshot_as_png())
def teardown(self):
self.driver.quit()
if __name__ == '__main__':
shot=TestScreenShot()
shot.test_screen()
运行结果:
来源:https://blog.csdn.net/aovenus/article/details/121180311
标签:python,自动化测试,selenium,屏幕截图
0
投稿
猜你喜欢
一文读懂吞吐量(TPS)、QPS、并发数、响应时间(RT)概念
2022-04-06 14:58:25
写给新手同学的vuex快速上手指北小结
2024-04-27 16:03:33
python之tensorflow手把手实例讲解猫狗识别实现
2021-12-26 14:46:11
python文件处理详解
2023-11-01 21:48:34
UltraEdit编辑器免费激活方法
2023-09-14 22:19:33
FCKeditor ASP.NET 上传附件研究
2023-12-30 05:27:22
20行Python代码实现视频字符化功能
2023-01-08 21:17:02
ASP.NET中的几种弹出框提示基本实现方法
2023-07-13 00:23:50
django 消息框架 message使用详解
2021-06-21 17:22:29
Python利用Seaborn绘制多标签的混淆矩阵
2022-12-12 17:46:22
略谈美国雅虎首页改版
2009-07-26 18:34:00
pytorch实现用Resnet提取特征并保存为txt文件的方法
2023-04-10 17:21:09
深入理解 ES6中的 Reflect用法
2024-04-16 09:30:44
Python使用itchat模块实现简单的微信控制电脑功能示例
2023-01-06 14:56:44
Django框架中模型的用法
2022-02-15 00:34:14
python面向对象入门教程之从代码复用开始(一)
2022-07-17 21:54:13
flask框架配置mysql数据库操作详解
2024-01-14 05:31:57
python 如何引入协程和原理分析
2022-09-06 11:26:07
一文初探Go语言中的reflect反射包
2024-02-07 01:31:51
MySQL隐式类型转换导致索引失效的解决
2024-01-12 22:45:25