Alerts in Selenium Python

An alert on the webpage used to get the attention of the user to perform some operation on the alert or the webpage; sometimes alerts expect input from the user. All the alerts are formed using javascript inside the HTML page; there are three kinds of alerts in HTML.

Alerts take the focus away from the current window and force the browser to read the message.
Alert prevents the user from accessing other parts of the web page until the box is closed.

Alert Types :

  • Alert
  • Confirmation dialog
  • Prompt
Properties of Alerts with respect to Selenium Python
  • We cannot identify alerts using inspect tools
  • We cannot write xpaths for alerts
  • It is not a Window
  • We cannot handle alerts using javaScript Executor.
  • We can consider not alert if any of the above property mismatches.

Code of Alert handling in selenium Python

When an alert is present on the webpage, we cannot proceed further without handling the popup, and if we try to perform any operation, it throws UnexpectedAlertPresentException.

We can handle alerts using the switch_to_alert() method present in selenium python; with the help of this switch_to_alert() method, we can handle the alerts created using javascript.

Syntax to handle Alert

ale = driver.switch_to_alert();

With this alerts API, we can perform the below operations on the pop-up.

1. Accept the popUp by clicking the OK button

ale.accept();

2. Dismiss the popUp by clicking the 'X' icon.

ale.dismiss();

3. Get popUp text by getText method

ale.text;

4. Send a text to popUp by send_keys (Applicable for Prompt only)

ale.send_keys("test Text");
Selenium bindings never click the Cancel button on any kind of alert

When do alerts occur on a page: It can occur on a page at any time, but most of it happens on the below timing of the webpage

  • On Webpage Load
  • On Webpage close
  • On click of an element
  • On Right-click (when right-clicking disabled)
  • On wrong entry of a field
  • On saving of information
Technical Tip: Nowadays, developers are using overlays/hidden division popups rather than javascript popups.

Handle Multiple windows in Selenium

Alert handling in python selenium

The alert() method displays an alert box with a message and an OK button, an alert box is often used to inform a user about a particular operation like details saved in Database, right-click disabled, Loaded successfully such kind of messages.

Alert is formed using alert("message") in javascript, alert considers the same irrespective of user operation whether he clicks OK or 'X' icon. Alert's sole purpose is to inform the user, nothing more.

Alert on Different browsers

Chrome:

alert-on-chrome-selenium-webdriver

Firefox:

alert-on-firefox-selenium-webdriver

Steps:
  1. Open Url : https://chercher.tech/java/practice-pop-ups-selenium-webdriver
  2. Click on the Alert button, and the application throws an Alert box
  3. Switch to the alert using driver.switchTo().alert();, and store the object
  4. We can accept the alert by using accept() function.
    def test_open_alerts(self):
    	driver = webdriver.Chrome(executable_path=r'D:PATHchromedriver.exe')
    	driver.get("https://chercher.tech/java/practice-pop-ups-selenium-webdriver");
    	driver.find_element_by_name("alert").click();
    	ale = driver.switch_to_alert()
    	# clicks 'OK' button
    	ale.accept();​​
  5. We can dismiss the popup using the dismiss() method from alert API, this click 'X' icon on the popup. (if you have performed step 4, you cannot perform this step as the pop-up is closed in step 4 itself).
    ale = driver.switch_to_alert();
    # clicks 'x' icon
    ale.dismiss();​
  6. We can get the text from the pop up using text; if you have performed step 4 or 5, this step will not work
    ale = driver.switch_to_alert();
    # clicks 'x' icon
    ale.text;

get-text-alert-selenium-webdriver

Handle Iframes in Selenium python

Confirmation Box

A confirmation box is the second kind of alert; it displays a dialog with the OK and Cancel button. The confirmation box informs the developer about user choice whether the user pressed OK or Cancel.

The confirm() method returns true if the user clicked "OK", and false otherwise('X' icon and 'Cancel') to the developer.

Confirmation box

confirm-chrome-selenium-webdriver

We can handle the Confirmation box in selenium python like an alert box, there is no coding difference.

Api Testing in Selenium Python

Prompt

Prompt is used to get value from the user in text format. Prompt provides text bar for user input; Prompt is rarely used alert type.

Prompt :
prompt-selenium-webdriver
Prompt also follows the same coding as alert and prompt except the sendkeys method, and we can send a text to prompt text bar using the sendkeys() method in alerts API.

You cannot use the Keys.ENTER or such kind of keys with send_keys() method of alerts

driver.get("https://chercher.tech/java/practice-pop-ups-selenium-webdriver");
driver.find_element_by_name("prompt").click();
ale = driver.switch_to_alert();
ale.send_keys("chercher.tech");

All kind of xpaths in Selenium Python

About Author :

I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
  • gilberto
    thanks four your golden hints about webdriver using key down and control key plus any key. i using this to handle multiples clipboards on windows (like clipclip, ditto, etc). very well done. thanks from são paulo - sp - brasil.
    Reply