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)
# Truncate the current window and specify where to save the picture
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 () #如果不存在, creating a folder
While Path (Wholepath). Exists (): #判断文件是否已经存在, you can also use Is_file () to determine
I+=1
Capturename = ' \ \ ' +str (i) + '. png '
Wholepath = Scrpath+capturename
Driver.get_screenshot_as_file (Wholepath) #不能接受Path类的值, can only be a string, otherwise you cannot
Sleep (5)
Driver.quit ()
Selenium+python+webdriver: Save to the specified folder