Ideas:
1 new screenshot folder does not exist, create it
2 In the screenshot folder New folder today's date, for example, 20,190,110; it does not exist create it
Screenshot 3 to save the day folder, the file name with a time stamp and the screenshot. To avoid performing multiple use cases, the screenshot is covered.
import os from datetime import datetime DEF getscreenshot (Driver, file_name = " page screenshot " ): screenshot_dir ='./screenshot' nowdate = datetime.now().strftime('%y%m%d') # 当日日期 screenshot_today_dir = os.path.join(screenshot_dir, nowdate) IF not os.path.exists (screenshot_dir): # does not exist, create the directory os.mkdir (screenshot_dir) if not os.path.exists(screenshot_today_dir): os.mkdir(screenshot_today_dir) nowtime = datetime.now().strftime('%H%M%S%f') filename=nowtime+file_name+".png" filepath=os.path.join(screenshot_today_dir,filename) driver.get_screenshot_as_file (filepath) # Screenshot, filename = filename + timestamp the else : nowtime = datetime.now().strftime('%H%M%S%f') filename = nowtime + file_name + ".png" filepath = os.path.join(screenshot_today_dir, filename) driver.get_screenshot_as_file (filepath) # Screenshot, filename = filename + timestamp the else : IF not os.path.exists (screenshot_today_dir): os.mkdir(screenshot_today_dir) nowtime = datetime.now().strftime('%H%M%S%f') filename=nowtime+file_name+".png" filepath=os.path.join(screenshot_today_dir,filename) driver.get_screenshot_as_file (filepath) # Screenshot, filename = filename + timestamp the else : nowtime = datetime.now().strftime('%H%M%S%f') filename = nowtime + file_name + ".png" filepath = os.path.join(screenshot_today_dir, filename) driver.get_screenshot_as_file (filepath) # Screenshot, filename = filename + timestamp
4 Call
#coding =utf-8 from the Selenium Import webdriver Import Time from yishangdai_web.testcase. screenshot stamped Import getscreenshot driver = webdriver.Chrome() driver.get("http://www.baidu.com") time.sleep(3) getscreenshot(driver) driver.quit()
After running: