Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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")

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading