title: Try Except Statement to Test for Presence of Error Button, Throw Exception if Element Found
body: <p>I have written a Python / Selenium Script to automate a work flow on a financial site.</p>
  
  <p>One of the tests involves checking for the presence of error text on one of the screens.</p>
  
  <p>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.</p>
  
  <p>I wrote code that identifies the error button. I then put this code into a Try / Except Block.</p>
  
  <p>However, I have questions on how to improve this error handling structure (if it can be improved).</p>
  
  <pre><code>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
  </code></pre>
  
  <p>When I run the code and the error exists, I get an "AssertionError" (expected), screenshot is taken (expected) and script stops (expected).</p>
  
  <p>However, I feel like I am missing something.</p>
  
  <p>Is there a better way to check for the presence of the error button?</p>