tajir12457
tajir12457

Reputation: 153

Creating a new folder each time Selenium takes a screenshot

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?

Upvotes: 0

Views: 463

Answers (2)

tajir12457
tajir12457

Reputation: 153

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)

Upvotes: 1

Prophet
Prophet

Reputation: 33371

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

Upvotes: 1

Related Questions