Notes on screenshot method get_screenshot_as_file()

The following symbols / are not allowed in the screenshot method: * ? # ” < > | Otherwise, the screenshot will fail, so you can only change: to - 

def img_screenshot(self,Image_name):
    try:
       self.driver.get_screenshot_as_file('D:\workspace\thinksns\result\screenshot\fail\'+Image_name+'.jpg')
    except:
       self.log.error(u'截图失败:')

get_screenshot_as_file() As shown in the above code, the path to save the screenshot must be an absolute path. If the above is written as a relative path, the screenshot will fail! remember

If we want to add time to the screenshot name, it can be as follows:

def img_screenshot(self,Image_name):
        try:
            now = time.strftime('%Y-%m-%d_%H-%M-%S') #截图的方法中不允许存在以下符号 / : * ? # ” < > |
            self.driver.get_screenshot_as_file('D:\workspace\thinksns\result\screenshot\fail\'+Image_name+now+'.png') #这里路径必须为绝对路径不然截图失败
        except:
            self.log.error(u'截图失败:')


原本方法是这样写的time.strftime('%Y-%m-%d_%H:%M:%S') 但是因为截图的方法中不允许存在以下符号 / : * ? # ” < > |不然截图失败,所以只能将:改为-

The folder for saving screenshots needs to be created manually, and the code will not be created automatically

 # 截图
        if m == "450":
            # 需要手动新建文件夹shaoxing
            filename = f'./shaoxing/{n}.png'
        elif m == "680":
            filename = f'./quzhou/{n}.png'

        driver.get_screenshot_as_file(filename)

Guess you like

Origin blog.csdn.net/weixin_42550871/article/details/127726585