How to Check if any alert exists in selenium using Python

0 votes

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.

May 10, 2018 in Selenium by eLiJha
• 770 points
12,586 views

1 answer to this question.

0 votes

This might work for you. It did for me. I set a conditional delay with WebDriverWait just before the point I expect the alert to popup. Then I switch to it, like this:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

browser = webdriver.Firefox()
browser.get("url")
browser.find_the_element_by_id("add_button").click()

try:
    WebDriverWait(browser, 3).until(EC.alert_is_present(),
                                   'Timed out waiting for PA creation ' +
                                   'confirmation popup to appear.')

    alert = browser.switch_to.alert
    alert.accept()
    print("alert accepted")
except TimeoutException:
    print("no alert")

This command WebDriverWait(browser,3) will wait for atleast 3 seconds for a supported alert to appear.

answered May 10, 2018 by king_kenny
• 3,710 points

Related Questions In Selenium

0 votes
1 answer

How to upload a resume to a website using selenium in python?

Use this code, this will help you: from ...READ MORE

answered Apr 20, 2018 in Selenium by Vardy
• 2,360 points
2,588 views
0 votes
2 answers
0 votes
1 answer
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
12,710 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
8,607 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
6,680 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,550 views
+10 votes
17 answers

How to automate gmail login process using selenium webdriver in java?

Check the below code: Here is the working ...READ MORE

answered Apr 24, 2018 in Selenium by Vardy
• 2,360 points
194,131 views
0 votes
1 answer

Is there a way to pass on the options/ flags to Selenium if i'm scripting in Python?

This is the usage: from selenium import webdriver from ...READ MORE

answered Jun 8, 2018 in Selenium by king_kenny
• 3,710 points
4,617 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP