这篇文章主要为大家展示了“python如何自动化测试selenium屏幕截图”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“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()
运行结果:
以上是“python如何自动化测试selenium屏幕截图”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。