python selenium 截图

from seleniumimport webdriver

import time

import os

# 图片时间戳  strftime()获取当前截图

image_time= time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))

# 目录时间戳

directory_time= time.strftime("%Y-%m-%d", time.localtime(time.time()))

print(image_time)

print(directory_time)

# 获取当前文件目录os.getcwd()

print(os.getcwd())

try:

    file_path= os.getcwd() + "\\" + directory_time+ "\\"

    if not os.path.exists(file_path):

        os.makedirs(file_path)        # os.makedirs() 新建文件夹

        print("新建目录成功:%s" % file_path)

    else:

        print("目录已存在")

except BaseException as msg:

    print("新建目录失败 %s" % msg)

driver= webdriver.Chrome()

driver.get('http://khtest.test.dc-it.cn/resource/#/index')

# 截图

try:

    file_name= driver.save_screenshot('.\\' + directory_time+ '\\' + image_time+ '.png')

    print("%s:截图成功!!!" % file_name)

except BaseException as image_msg:

    print("截图失败:%s" % image_msg)

time.sleep(2)

driver.quit()

你可能感兴趣的:(python selenium 截图)