Home > Net >  Creating a new folder each time Selenium takes a screenshot
Creating a new folder each time Selenium takes a screenshot

Time:02-16

I'm using Python Robot framework Selenium to take screenshots of a few urls. This is the code:

output = self.builtin.get_variable_value("${OUTPUT DIR}")
time = datetime.now().strftime("%Y-%m-%d %H.%M.%S")
folder = "New_folder"   time
path = os.path.join(output, folder)
for url in url_list:
    driver.get(url)
    driver.save_screenshot(path   "/"   url   '.png')

The idea is to create a new, unique folder each time the code runs, and saves the screenshots inside it. But it doesn't work and the folder is not created. What should I do?

CodePudding user response:

Looks like you are mixing variables names here.
Instead of

time = datetime.now().strftime("%Y-%m-%d %H.%M.%S")
folder = "New_folder"   now

It should be

time = datetime.now().strftime("%Y-%m-%d %H.%M.%S")
folder = "New_folder"   time

While time is not a good name for a variable.
I'd advice to use something like:

current_time = datetime.now().strftime("%Y-%m-%d %H.%M.%S")
folder = "New_folder"   current_time

CodePudding user response:

OK I found the problem. I had to generate the whole path before `save_screenshot, and create the folder. Something like this:

path = os.path.join(output, folder)
os.makedirs(path)
for url in url_list:
    driver.get(url)
    file_address = path   "/"   url   '.png'
    driver.save_screenshot(file_address)
  • Related