selenium實現螢幕截圖

吃喝玩樂秀起來發表於2020-12-21

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('http://www.baidu.com')
        self.driver.maximize_window()

    def test_jieping(self):
        self.driver.find_element_by_id('kw').send_keys('selenium')
        self.driver.find_element_by_id('su').click()
        sleep(2)
        #現在截圖儲存在當前路徑
        #self.driver.save_screenshot('baidu.png')

        #截圖的名稱拼接上日期
        #st=strftime("%Y-%d-%H-%M-%S",localtime(time()))
        #fime_time=st+'.png'
        #self.driver.save_screenshot(fime_time)

        #截圖儲存到具體目錄下
        st = strftime("%Y-%d-%H-%M-%S", localtime(time()))
        fime_time=st+'.png'
        path=os.path.abspath('screnshopt')
        file_path=path+'/'+fime_time
        self.driver.get_screenshot_as_file(file_path)

if __name__ == '__main__':

    case=TestCase()
    case.test_jieping()

相關文章