# selenium中对截屏的操作
from selenium import webdriver
from time import sleep, strftime, localtime, time
import os
class TestCase(object):
# 初始化
def __init__(self):
self.driver = webdriver.Chrome()
self.driver.get('https://www.baidu.com')
self.driver.maximize_window()
# 初步截屏
def test_screen(self):
self.driver.find_element_by_id('kw').send_keys('中国移动云视讯')
self.driver.find_element_by_id('su').click()
# 生成截图
self.driver.save_screenshot('baidu.png')
# 将截屏文件名称改为当前本地时间时分秒
def test_filetime_screen(self):
self.driver.find_element_by_id('kw').send_keys('中国移动云视讯')
self.driver.find_element_by_id('su').click()
# 生成截图
st = strftime("%Y-%m-%d-%H-%M_%S",localtime(time()))
file_time = st +'.png'
sleep(2)
self.driver.save_screenshot(file_time)
# 将截屏文件保存到文件夹中
def filetime_folder_screen(self):
self.driver.find_element_by_id('kw').send_keys('中国移动云视讯')
self.driver.find_element_by_id('su').click()
st = strftime("%Y-%m-%d-%H-%M_%S", localtime(time()))
file_name = st + '.png'
path = os.path.abspath('img')
file_path = path+'/'+file_name
print(file_name)
print(file_path)
sleep(1)
res = self.driver.get_screenshot_as_file(file_path)
# if res:
# self.driver.get_screenshot_as_file("C://Users/Administrator/PycharmProjects/Demo1/img/filetime2.png")
# print("失败")
# else:
# print("成功")
if __name__ == '__main__':
case = TestCase()
# case.test_screen()
# case.test_filetime_screen()
case.filetime_folder_screen()
sleep(2)
case.driver.quit()