I'm trying to write a test with selenium in python language for a web page that manages users. In this page, someone can add a role for users and if a role exists while adding it, an alert raises. I don't know if the alert is a javascript alert or an element of the web page. I want to automatically check the existence of the alert, because checking for the role in the list wastes time and has an enormous load. I tried this:
browser = webdriver.Firefox()
browser.get("url")
browser.find_the_element_by_id("add_button").click()
try:
alert = browser.switch_to_alert()
alert.accept()
print "alert accepted"
except:
print "no alert"
But it didn't work and I got the "UnexpectedAlertPresentException". I also tried this:
browser = webdriver.Firefox()
browser.get("url")
browser.find_the_element_by_id("add_button").click()
s = set(browser.window_handles)
s.remove(browser.current_window_handle)
browser.switch_to_window(s.pop())
But I got the same exception. Additionally, I tried to access the alert with firebug to check if I can get access with its properties, but right-click was disabled. I need a solution very quickly, even in other languages. I can understand the approach anyway. I will appreciate any help.