Python selenium save screenshot in newly created folder
Question:
I would like to create a folder where screenshots will be saved when a test fails, it would be great if the folder is only created when there is a test failure.
This is currently not working, it is not saving the screenshot inside the folder:
try:
os.makedirs('./screenshots')
except OSError:
pass
def check_exists_by_xpath(xpath):
try:
driver.find_element_by_xpath(xpath)
except NoSuchElementException:
return False
driver.save_screenshot('screenshots/screenie.png')
return True
Answers:
This is because save_screenshot()
call is unreacheable, the function returns before making a screenshot. The fixed version:
def check_exists_by_xpath(xpath):
try:
driver.find_element_by_xpath(xpath)
except NoSuchElementException:
driver.save_screenshot('screenshots/screenie.png')
return False
return True
class saveScreen:
def __init__(self):
self.image_no = 0
def save(self,driver):
self.image_no+=1
driver.save_screenshot("./screenshots/images"+str(self.image_no)+".png")
obj = saveScreen()
obj.save(driver)
I would like to create a folder where screenshots will be saved when a test fails, it would be great if the folder is only created when there is a test failure.
This is currently not working, it is not saving the screenshot inside the folder:
try:
os.makedirs('./screenshots')
except OSError:
pass
def check_exists_by_xpath(xpath):
try:
driver.find_element_by_xpath(xpath)
except NoSuchElementException:
return False
driver.save_screenshot('screenshots/screenie.png')
return True
This is because save_screenshot()
call is unreacheable, the function returns before making a screenshot. The fixed version:
def check_exists_by_xpath(xpath):
try:
driver.find_element_by_xpath(xpath)
except NoSuchElementException:
driver.save_screenshot('screenshots/screenie.png')
return False
return True
class saveScreen:
def __init__(self):
self.image_no = 0
def save(self,driver):
self.image_no+=1
driver.save_screenshot("./screenshots/images"+str(self.image_no)+".png")
obj = saveScreen()
obj.save(driver)