Webdriver Screenshot

后端 未结 12 841
南方客
南方客 2020-11-30 00:57

When taking a screenshot using Selenium Webdriver on windows with python, the screenshot is saved directly to the path of the program, is there a way to save the .png file t

12条回答
  •  庸人自扰
    2020-11-30 01:43

    Sure it isn't actual right now but I faced this issue also and my way: Looks like 'save_screenshot' have some troubles with creating files with space in name same time as I added randomization to filenames for escaping override.

    Here I got method to clean my filename of whitespaces (How do I replace whitespaces with underscore and vice versa?):

    def urlify(self, s):
        # Remove all non-word characters (everything except numbers and letters)
        s = re.sub(r"[^\w\s]", '', s)
        # Replace all runs of whitespace with a single dash
        s = re.sub(r"\s+", '-', s)
        return s
    

    then

    driver.save_screenshot('c:\\pytest_screenshots\\%s' % screen_name)
    

    where

    def datetime_now(prefix):
        symbols = str(datetime.datetime.now())
        return prefix + "-" + "".join(symbols)
    
    screen_name = self.urlify(datetime_now('screen')) + '.png'
    

提交回复
热议问题