Selenium+python+webdriver: Save screenshots to the specified folder

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.