I've written a selenium test case in python for a web page that manages users. Here, we can add a role for users and in case a role is already existing, then an alert araises. I'm not sure if the alert is a JavaScript alert or a webelement of the page itself. I want to automatically check the existence of the alert, because checking for the role from the entirety of the list is a waste of time and has an enormous load. I've tried this code:
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 that didn't work. I got the error "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())
And I still got the same error. Moreover, I checked the alert box with firebug to check if I can get access to its properties, but right click was disabled. I need an immediate solution. Even if its in Java or other languages, I'll figuring things out. Someone plz help me out.