Selenium_截图操作
from selenium import webdriver
import time driver = webdriver.Chrome("D:\脚本项目\lianxi\chromedriver.exe") url = "http://www.baidu.com" driver.get(url) driver.maximize_window() driver.find_element_by_id('kw').send_keys("selenium") driver.find_element_by_id("su").click() time.sleep(1) #生成日月时分秒时间 picture_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(picture_time) print(directory_time)#获取到当前文件的目录,并检查是否有directory_time文件夹,如果不存在则自动新建directory_time文件夹 try: File_Path = os.path.join(os.getcwd(), directory_time) if not os.path.exists(File_Path): os.makedirs(File_Path) print("目录新建成功:%s" % File_Path) else: print("目录已存在!!!") except BaseException as msg: print("新建目录失败:%s" % msg) try: url = driver.get_screenshot_as_file(".\\"+ directory_time +"\\"+picture_time+".png") print("%s :截图成功!!!" % url) except BaseException as pic_msg: print("截图失败:%s" % pic_msg) time.sleep(1) driver.quit()
主要是使用get_screenshot_as_file()函数