How to handle Alerts and Popup windows using Selenium WebDriver


Alert is a message box which displays on-screen notification to give the user some kind of information or ask for permission to perform certain kind of operation.
To handle popups and alerts using selenium web driver, you need to switch to alert window.
There are two types of alerts
1)   Windows based
2)   Browser/Web Based.

Windows based alert messages we can’t handle through selenium web driver. You can handle through “AutoIT”(we will cover later).

we are going to discuss on web based alert messages, because selenium web driver can handle only web based alert messages.
Different type of web based alerts windows.

1)   Simple Alert: It will display simple information and one button.
2)   Prompts: it will ask some input from user to perform some action.
3)   Confirmation: it will ask confirmation from user and do some action based on user information.
While running the WebDriver script, the driver control will be on the browser even after the alert generated which means the driver control will be behind the alert pop up. In order to switch the control to alert pop up, we use the following command.

driver.switch_to.alert 
Once we switch the control from browser to the alert window. We can use the methods to do required actions such as accepting the alert, dismissing the alert, get the text from the alert window, writing some text on the alert window etc.,

Below are the Methods to handle alert windows.
1)   Dismiss(): it will click on cancel button when alert appears.
2)   Accept(): it will click on OK/Yes button when alert appears.
3)   Text(): it will capture text message which is present on alert window.
4)   Send_keys(): It will enter the text into the alert message.

Scenario -1 :
Invoke Google Chrome browser.
Click on sign in link.
Click on Go button with out entering username and password.
print text message which is present on alert and  click on OK button.

Script :
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
chromePath=
"C:\\BrowserDriver\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get(
"https://www.rediff.com/")
driver.find_element_by_xpath("//a[@class='signin']").click()
driver.find_element_by_xpath(
"//input[@type='submit' and @value='Go']").click()
# text method is used to capture the text on alert.
print(driver.switch_to.alert.text)
# accept() method is used to click on OK button.
driver.switch_to.alert.accept()


Scenario -2:
Invoke Google Chrome browser.
Click on Try it button.
Enter some text on prompt window(Ex: Python Selenium).
Click on OK button.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
chromePath=
"C:\\BrowserDriver\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get(
"https://www.w3schools.com/js/tryit.asp?filename=tryjs_prompt")
driver.switch_to.frame(
"iframeResult")
driver.find_element_by_xpath(
"//button[@onclick='myFunction()']").click()
driver.switch_to.alert.send_keys(
"Python Selenium")
driver.switch_to.alert.accept()
print(driver.find_element_by_xpath("//p[@id='demo']").text)
driver.switch_to.default_content()

Scenario -3:
Invoke Google Chrome browser.
Click on Try it button.
Click on OK button.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
chromePath=
"C:\\BrowserDriver\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get(
"https://www.w3schools.com/js/tryit.asp?filename=tryjs_prompt")
driver.switch_to.frame(
"iframeResult")
driver.find_element_by_xpath(
"//button[@onclick='myFunction()']").click()
print(driver.switch_to.alert.text)
# click on OK Confirmation button
driver.switch_to.alert.accept()
driver.switch_to.default_content()

Scenario -4:
Invoke Google Chrome browser.
Click on Try it button.
Click on OK button.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
chromePath=
"C:\\BrowserDriver\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get(
"https://www.w3schools.com/js/tryit.asp?filename=tryjs_prompt")
driver.switch_to.frame(
"iframeResult")
driver.find_element_by_xpath(
"//button[@onclick='myFunction()']").click()
print(driver.switch_to.alert.text)

# click on Cancel Confirmation button
driver.switch_to.alert.dismiss()
 
print(driver.find_element_by_xpath("//p[@id='demo']").text)
driver.switch_to.default_content()

In the above scripts(2,3 and 4) we are using switch_to.frame along with alert because “Try it “ button is inside the frame.

Note:
1) If alert is not present in the window and still we try to switchTo alert window then Selenium will throw NoAlertPresentException
2) Web-Based alert and Java Script alerts are same so do not get confused.
3) Until you do not handle alert window you cannot perform any action in the parent window.

Comments