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

Time:12-21

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

CodePudding user response:

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.

  • Related