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屏幕截圖示例的詳細內容,更多關於selenium屏幕截圖示例的資料請關註WalkonNet其它相關文章!
推薦閱讀:
- python自動化測試selenium核心技術三種等待方式詳解
- python編程使用selenium編寫測試用例
- python自動化測試selenium執行js腳本實現示例
- Python截圖並保存的具體實例
- python自動化測試selenium核心技術等待條件教程