保存图片到指定文件夹:
from selenium import webdriver
from pathlib import Path
from time import sleep
driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
driver.find_element_by_id('kw').send_keys('selenium')
driver.find_element_by_id('su').click()
sleep(5)
# 截取当前窗口,并指定截图图片的保存位置
i=1
scrpath="C:\Users\whu024\Desktop\AmyPython" #指定的保存目录
capturename = '\'+str(i) + '.png' #自定义命名截图
wholepath=scrpath+capturename
print (wholepath)
if Path(scrpath).is_dir(): #判断文件夹路径是否已经存在
pass
else:
Path(scrpath).mkdir() #如果不存在,创建文件夹
while Path(wholepath).exists(): #判断文件是否已经存在,也可使用is_file()判断
i+=1
capturename = '\'+str(i) + '.png'
wholepath = scrpath+capturename
driver.get_screenshot_as_file(wholepath) #不能接受Path类的值,只能是字符串,否则无法截图
sleep(5)
driver.quit()
保存图片到Excel:
import xlsxwriter
book = xlsxwriter.Workbook('C:AmyPersonalAmyMoKuaipict.xlsx')
sheet = book.add_worksheet('demo')
sheet.insert_image('A1','0.jpg')
book.close()
(pip install XlsxWriter)