title: Python / Selenium : Switch to an alert and verify the text within
body: <p>My issue: Being able to detect an alert box has been opened, verify the text inside the alert, confirm the text and then close the alert. Then, go back and correct the invalid email.</p>
  
  <p>My test is one for registering a user. I need to validate when someone might input incorrect data or mismatched data. In this case, I am entering an email and verifying the email first entered. Should they have a mismatch, a popup will appear alerting the user to check the email addresses they have entered and that they match. So far all I can get is an error.</p>
  
  <p>Error:</p>
  
  <blockquote>
    <p>E       UnexpectedAlertPresentException: Message: u'Modal dialog
    present' ; Stacktrace: Method nsCommandProcessor.prototype.execute
    threw an error in file:///var/folders/1j/x3cxkrqn3sscdyq0t36169hr0000gn/T/tmpTCqMgm/extensions/fxdriver@googlecode.com/components/command_processor.js</p>
  </blockquote>
  
  <p>I thought my code would handle this, but nope. If someone could point out my, obvious to you, mistake I would be grateful.</p>
  
  <p>My entire test code:</p>
  
  <pre><code>from selenium import webdriver
  from selenium.webdriver.common.by import By
  from selenium.webdriver.support.ui import Select
  from selenium.common.exceptions import NoSuchElementException
  import unittest, time, re
  
  class ChallengeTests(unittest.TestCase):
      def setUp(self):
          self.driver = webdriver.Firefox()
          self.driver.implicitly_wait(5)
          self.base_url = "https://www.testpage.com"
          self.verificationErrors = []
          self.accept_next_alert = True
  
  # SIGN UP NEW USER
  
      def test_00_sign_up(self):
          driver = self.driver
          driver.get(self.base_url + "/")
          driver.find_element_by_id("remail").send_keys("foobar@me.com")
          driver.find_element_by_id("remail_confirm").send_keys("bar@me.com")
          driver.find_element_by_id("next").click()
          alert = self.driver.switch_to_alert()
          alert = self.assertTrue(self.is_text_present("The email addresses you entered"))
          driver.find_element_by_id("remail_confirm").send_keys("foobar@me.com")
          driver.find_element_by_id("registration_button").click()
  
  # NON TESTS
  
      def is_element_present(self, how, what):
          try:
              self.driver.find_element(by=how, value=what) # to find page elements
          except NoSuchElementException, e: return False
          return True     
  
      def is_text_present(self, text):
          try:
              body = self.driver.find_element_by_tag_name("body") # find body tag element
          except NoSuchElementException, e: return False
          return text in body.text # check if the text is in body's text
  
      def is_alert_present(self):
          try:
              self.driver.switch_to_alert()
          except NoAlertPresentException, e: 
              return False
  
      def close_alert_and_get_its_text(self):
          try:
              alert = self.driver.switch_to_alert()
              alert_text = alert.text
              if self.accept_next_alert:
                  alert.accept()
              else:
                  alert.dismiss()
              return alert_text
          finally: self.accept_next_alert = True        
          return True
  
      def tearDown(self):
          self.driver.quit()
          self.assertEqual([], self.verificationErrors)
  </code></pre>