I have written a Python / Selenium Script to automate a work flow on a financial site.
One of the tests involves checking for the presence of error text on one of the screens.
Attached is a Try / Except block that checks for the presence of a button (id = errorShowMore). If the button displays, I want the script to Fail and I want to take a screenshot of the page.
I wrote code that identifies the error button. I then put this code into a Try / Except Block.
However, I have questions on how to improve this error handling structure (if it can be improved).
try:
showMoreButton = driver.find_element_by_id("errorShowMore")
assert showMoreButton.click() except AssertionError:
screenshot_name = "FAIL" + "_" + test_case_ID + "_" + browser + "_" + env + "_" + time_stamp + ".png"
saved_screenshot_location = str(screenshot_directory / screenshot_name)
driver.get_screenshot_as_file(saved_screenshot_location)
raise
When I run the code and the error exists, I get an "AssertionError" (expected), screenshot is taken (expected) and script stops (expected).
However, I feel like I am missing something.
Is there a better way to check for the presence of the error button?
You can simply use find_elements_by_id
and get the count.
errEleCount = len(driver.find_elements_by_id("errorShowMore"))
if errEleCount>0:
# implement the logic here.