Cassano
Cassano

Reputation: 323

How do I save multiple screenshots with same name in Selenium?

So I'm trying to save a screenshot with the same name for example, "Screen" and then if it already exists, save as "Screen1" and "Screen2" and so on.

This is my code:

driver.get_screenshot_as_file("Screen.png")

Upvotes: 0

Views: 597

Answers (1)

Frederick
Frederick

Reputation: 470

Here you can find more information about it. You could use a while loop and check for every name (Screen1, Screen2, ...), whether it exists or not. A short example:

import os.path

i = 1
while True:
    fname = "Screen" + str(i) + ".png"
    if not os.path.isfile(fname):
        break
    i += 1
print(fname)

You could also store the current i and use it when saving a screenshot, this might be more efficient than this approach.

Upvotes: 1

Related Questions