python自动化测试selenium屏幕截图示例
程序员文章站
2022-07-03 22:27:03
webdriver内置了测试中捕获屏幕并保存的方法。示例脚本:(1)save_screenshot(filename):保存屏幕截图from selenium import webdriverfrom...
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()
运行结果:
以上就是python自动化测试selenium屏幕截图示例的详细内容,更多关于selenium屏幕截图示例的资料请关注其它相关文章!
上一篇: dubbo源码学习(二) : spring 自定义标签
下一篇: 他打呼噜声是出了名的
推荐阅读
-
小白学 Python 爬虫:自动化测试框架 Selenium 从入门到实战
-
Python Selenium框架设计使自动化测试报告的生成
-
Selenium3 + Python3自动化测试系列六——等待方式
-
selenium+python自动化测试
-
python 配置web自动化测试框架 selenium
-
Python+Selenium+Unittest+Ddt+HTMLReport分布式数据驱动自动化测试框架结构
-
Python http接口自动化测试框架实现方法示例
-
Python多线程应用于自动化测试操作示例
-
Selenium3 + Python3自动化测试系列四——鼠标事件和键盘事件
-
Python+Selenium - Web自动化测试-元素定位