Alerts & popup windows are major elements in web apps, and they often need some special handling in automated tests. Moreover, Selenium WebDriver offers a means of handling alerts & popup windows utilizing the Alert interface.
What’s Alert in Selenium?
Contents
In Selenium, an alert refers to a popup dialog box that appears on a web page to offer details or to prompt the user for some input. Moreover, alerts are created using JavaScript & are commonly utilized for displaying confirmation messages, error messages, or for receiving input from the user.
In addition, Selenium offers a means of handling alerts utilizing the Alert interface. The Alert interface offers a way of interacting with the alert like dismissing or accepting the alert, getting the text of the alert, or sending a text to the alert.
Alerts Types in Selenium
There are three varieties of alerts in Selenium. They include:
Simple Alert: This is the most basic kind of alert, which shows a message to a user & needs the user to click the OK button to dismiss it. The alert doesn’t have any input fields or other buttons apart from the OK button.
Example of Simple Alert:
from selenium import webdriver driver = webdriver.Chrome() # or whichever browser you prefer driver.get('https://www.example.com') # replace with the URL of your choice # find an element on the page that you want to interact with element = driver.find_element_by_id('example-element') # click the element to trigger the alert element.click() # switch to the alert and accept it alert = driver.switch_to.alert alert.accept() # close the browser driver.quit()
Confirmation Alert: The confirmation alert is just like a simple alert, but apart from having an OK button, it also comes with a cancel button. The user will either confirm or cancel an action depending on the message shown in the alert.
Example Confirmation Alert:
from selenium import webdriver driver = webdriver.Chrome() # or whichever browser you prefer driver.get('https://www.example.com') # replace with the URL of your choice # find an element on the page that you want to interact with element = driver.find_element_by_id('example-element') # click the element to trigger the confirmation alert element.click() # switch to the alert and dismiss it alert = driver.switch_to.alert alert.dismiss() # switch back to the default content driver.switch_to.default_content() # close the browser driver.quit()
Prompt Alert: This kind of alert is a kind of alert that shows a message to the user along with some input field & OK/Cancel buttons. Moreover, the user can enter some text in the input field & either cancel or confirm the action based on the input offered.
Example Prompt Alert
from selenium import webdriver driver = webdriver.Chrome() # or whichever browser you prefer driver.get('https://www.example.com') # replace with the URL of your choice # find an element on the page that you want to interact with element = driver.find_element_by_id('example-element') # click the element to trigger the prompt alert element.click() # switch to the alert and enter text into the prompt alert = driver.switch_to.alert alert.send_keys('Example text') alert.accept() # switch back to the default content driver.switch_to.default_content() # close the browser driver.quit()
Handling Alert in Selenium WebDriver
To handle alerts in Selenium, you first need to switch to the alert utilizing the switchTo() method of WebDriver class, & then interact with it utilizing the methods of Alert interface.
For instance
You need to use the accept() method to accept a confirmation alert:
Alert alert = driver.switchTo().alert(); alert.accept();
You can utilize the dismiss() method to dismiss a confirmation alert:
Alert alert = driver.switchTo().alert(); alert.dismiss();
To receive the text of an alert, one can utilize the getText() method:
Alert alert = driver.switchTo().alert(); String alertText = alert.getText(); System.out.println(alertText);
You can utilize the sendKeys() method to send a text to a prompt alert.
Alert alert = driver.switchTo().alert(); alert.sendKeys("Hello World!"); alert.accept();
Note that not every kind of alert accepts text input. Therefore, you should check the kind of alert before you try to send a text to it.
Handling Alert in Selenium Webdriver
Handling Pop-up windows utilizing Selenium Webdriver
Pop-up windows are browser windows that open at the top of the main browser window. Pop-up windows are utilized for different purposes like displaying login forms, advertisements, and other information.
To deal with pop-up windows utilizing Selenium WebDriver, you first require to switch to pop-up windows utilizing the WebDriver class get WindowHandles() method. This method gives a set of window handles for every window which are presently open in a browser. Moreover, you can then switch to a pop-up window by iterating the set of window handles & choosing the one which matches the handle of a pop-up window.
Once you’ve switched to a pop-up window, you can interact with it utilizing the methods of WebDriver class. For instance, you can receive the title of a pop-up window utilizing the getTitle() method:
String popUpWindowTitle = driver.getTitle(); System.out.println(popUpWindowTitle);
In addition, you can also relate to the elements on the pop-up window utilizing the same methods which you would utilize for the main window. For instance, you can get an element on a pop-up window utilizing the findElement() method:
WebElement popUpElement = driver.findElement(By.id("popUpElement")); popUpElement.click();
Switching back to the main window, you require to utilize the switchTo().defaultContent() method of the WebDriver class:
driver.switchTo().defaultContent();
This ensures that subsequent interactions using the web page are done in the major content area.
Below is an example of the way you can handle a pop-up message in Selenium WebDriver:
// Getting the current window handle String mainWindowHandle = driver.getWindowHandle(); // Clicking on the button which opens the pop-up window WebElement popUpButton = driver.findElement(By.id("popUpButton")); popUpButton.click(); // Waiting for the pop-up window to open WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(ExpectedConditions.numberOfWindowsToBe(2)); // Switch the pop-up window Set<String> windowHandles = driver.getWindowHandles(); for (String windowHandle : windowHandles) { if (!windowHandle.equals(mainWindowHandle)) { driver.switchTo().window(windowHandle); } } // Interacting with a pop-up window String popUpWindowTitle = driver.getTitle(); System.out.println(popUpWindowTitle); // Switching back to main window driver.switchTo().window(mainWindowHandle);
This code above will click on a button that opens a pop-up window, waits for the pop-up window to open, shift to the pop-up window receives the title of a pop-up window, & then switches back to the main window.
Handling Multiple Windows in Selenium
Output
After executing the above code, it will launch the site & on pressing the link “Click here,” it will open up a child window in a new tab. Moreover, one can also close the child window, & switch to the parent window the moment the operation is completed thus handling more than a single window in the app.
Terry White is a professional technical writer, WordPress developer, Web Designer, Software Engineer, and Blogger. He strives for pixel-perfect design, clean robust code, and a user-friendly interface. If you have a project in mind and like his work, feel free to contact him