最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
python自动化测试selenium屏幕截图代码示例
时间:2022-06-25 01:35:33 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下python自动化测试selenium屏幕截图代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
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()
运行结果:
相关文章
- 《无限暖暖》天星之羽获得位置介绍 12-20
- 《流放之路2》重铸台解锁方法介绍 12-20
- 《无限暖暖》瞄准那个亮亮的成就怎么做 12-20
- 《无限暖暖》魔气怪终结者完成方法 12-20
- 《无限暖暖》曙光毛团获得位置介绍 12-20
- 《无限暖暖》日光果获得位置介绍 12-20