Switch_To Commands

SwitchTo command in selenium helps the user to navigate to or to switch the controls of the selenium in to target object.

By changing the control to the target object, we will lose the control over the previous object.

SwitchTo helps to move the control over below items:

  • Element
  • Alerts
  • Frames
  • Tabs
  • Windows.

Alerts in Selenium Python

An alert in webpage used to get the attention of the user to perform some operation on the alert or on the webpage, sometimes alerts expects input from the user.

All the alerts are formed using javascript inside HTML page, there are three kind of alerts in HTML.

Alerts takes the focus away from the current window, and forces 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 :
1. Alert
2. Confirmation dialog
3. Prompt

Properties of Alerts with respect to Selenium Webdriver
1. We cannot identify alerts using inspect tools
2. We cannot write xpaths for alerts
3. It is not a Window
4. We cannot handle alerts using javaScript Executor.
We can consider as not alert if any of the above property mismatches.

Code of Alert handling in selenium webdriver

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

We can handle alerts using switch_to_alert() method present in selenium python, with 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 below opertaions on the pop up.
1. Accept the popUp by clicking OK button


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


3. Get popUp text by getText method


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

                        		ale.send_keys("test Text");

Note : Selenium bindings never clicks Cancel button on any kind of alert

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

  • On Webpage Load
  • On Webpage close
  • On click of a element
  • On Right click (when right click disabled)
  • On wrong entry of a field
  • On saving of information

Technical Tip : Now days developers are using overlays/hidden division popups rather than javascript popups


The alert() method displays an alert box with a message and an OK button, alert box is often used to inform a user about a partcular 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 same irrespective of user operation whether he clicks OK or 'X' icon.

Alert's sole purpose is to inform user, nothing more.

Alert on Different browsers
Chrome: alert-on-chrome-selenium-webdriver

Firefox alert-on-firefox-selenium-webdriver

1. Open Url : https://chercher.tech/java/practice-pop-ups-selenium-webdriver
2. Click on Alert button, application throws an Alert box 3. Switc to the alert using driver.switchTo().alert(), we save this object in Alert type variable 'ale'
4. We can accept the alert by using accept() non-static method from the alert api, this closes the popup.

def test_open_alerts(self):
	driver = webdriver.Chrome(executable_path=r'D:\PATH\chromedriver.exe')
	ale = driver.switch_to_alert()
	# clicks 'OK' button

5. We can dismiss the popup using dismiss() method from alert api, this click 'X' icon on popup. (if you have performed step 4, you cannot perform this step as pop up is closed in step 4 itself)

ale = driver.switch_to_alert();
# clicks 'x' icon

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


Confirmation Box

Confirmation box is second kind of alert, it displays a dialog with OK and Cancel button

Confirmation box informs developer about user choice whether user pressed OK or Cancel. The confirm() method returns true if the user clicked "OK", and false otherwise('X' icon and 'Cancel') to developer.

Confirmation box confirm-chrome-selenium-webdriver

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


Prompt is used to get value from the user in text format. Prompt provides textbar for user input, Prompt is rarly used alert type.

Prompt : prompt-selenium-webdriver

Prompt also follows same coding as alert and prompt except sendkeys method, we can send text to prompt textbar using sendkeys() method in alerts api.

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

ale = driver.switch_to_alert();

Complete code to handle Alerts

ale = driver.switch_to_alert();

You can also share knowledge by Adding a topic here

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.