selenium的屏幕截屏

WebDriver内置了一些捕获屏幕并保存的方法:
selenium的屏幕截屏_第1张图片
代码用例如下:

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')

    def test1(self): #获取当前时间为文件名称,并保存到当前目录
        self.driver.find_element_by_id('kw').send_keys('selenium')
        self.driver.find_element_by_id('su').click()

        sleep(2)
       
        st = strftime("%Y-%m-%d-%H-%M-%S", localtime(time()))
        file_name = st + '.png'
        self.driver.save_screenshot(file_name)

    def test2(self): #保存在某一个目录中
        st = strftime("%Y-%m-%d-%H-%M-%S", localtime(time()))
        fime_name = st + '.png'

        path = os.path.abspath('scrennshot')
        file_path = path+'/'+fime_name
        self.driver.get_screenshot_as_file(file_path)


if __name__ == '__main__':
    case = TestCase()
    #case.test1()
    case.test2()

你可能感兴趣的:(selenium,屏幕截屏)