Python+selenium realize screenshot image and save the captured image

  • 2020-06-23 00:53:39
  • OfStack

This article describes how to take screenshots using Selenium's method. During testing, it is necessary to take screenshots, especially when errors occur. There are three main screenshot methods in selenium for Python, and we chose the one that is most commonly used.

Screenshot skill should be one of the most important skills for testers.

In automated tests, screenshots help us visually locate errors and document test steps.

I remember that when I was working on an automation project for a multinational bank, PM of a bank required us to take at least one screenshot for every step of the automated test to prove that every function was covered by the automated test. In this case, the screenshot became an important means to prove the effectiveness of the automated test.

A good tester will get a good picture, just as a nerd will get a good poem.

webdriver's screenshots score 10 out of 10. In the past, the most troublesome problem with screenshots was that the page was so long that it could only reach 1 screen, and the area outside the screen that you had to move the scroll bar to see was usually inaccessible. Now webdriver has solved this problem, and no matter how long the page is, webdriver can perfectly intercept the complete page.

The following code demonstrates how to take screenshots using webdriver:

# -*- coding: utf-8 -*-
from selenium import webdriver
import unittest
import os,sys,time
import HTMLTestReport
# The login 
driver =webdriver.Firefox()
current_time = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
current_time1 = time.strftime("%Y-%m-%d", time.localtime(time.time()))
print(current_time )
print(current_time1 )
#  The image path must be printed HTMLTestRunner To capture and generate the path, \image\**\\**.png  Is the condition for getting the path , Must be such a directory 
# Set the path to store the images, and the test results can be differentiated by day 
# through if Make an assertion judgment 
# New create path" . "Stands for the present whole .py The location of the file's path," \\ "Path divider, where 1 One is" \ "Represents an escape character 
pic_path = '.\\result\\image\\' + current_time1+'\\' + current_time +'.png'
# Interception of the current url The image of the page and save the captured image under the specified path ( pic_path ), note: Either of the following methods is acceptable 
driver.save_screenshot('.\\result\\image\\' + current_time1+'\\' + current_time +'.png') 
if u' baidu 1 Well, you'll see ' == driver.title:
  print ('Assertion test pass.') 
  print ('Assertion test fail.')
 # through try Throws an exception for assertion determination   
  assert u' baidu 1 Well, you'll see ' == driver.title
  print ('Assertion test pass.') 
except Exception as e:
  print ('Assertion test fail.', format(e))

save_screenshot method realizes the screenshot function, only need to pass in the file name to save the screenshot, 10 minutes convenient.


Related articles: