Trending October 2023 # Selenium Alert &Amp; Popup Window Handling: How To Handle? # Suggested November 2023 # Top 11 Popular | Phuhoabeautyspa.com

Trending October 2023 # Selenium Alert &Amp; Popup Window Handling: How To Handle? # Suggested November 2023 # Top 11 Popular

You are reading the article Selenium Alert &Amp; Popup Window Handling: How To Handle? updated in October 2023 on the website Phuhoabeautyspa.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 Selenium Alert &Amp; Popup Window Handling: How To Handle?

What is Alert in Selenium?

An Alert in Selenium is a small message box which appears on screen to give the user some information or notification. It notifies the user with some specific information or error, asks for permission to perform certain tasks and it also provides warning messages as well.

In this tutorial, we will learn how to handle popup in Selenium and different types of alerts found in web application Testing. We will also see how to handle Alert in Selenium WebDriver and learn how do we accept and reject the alert depending upon the alert types.

Types of Alerts in Selenium 1) Simple Alert

The simple alert class in Selenium displays some information or warning on the screen.

2) Prompt Alert

This Prompt Alert asks some input from the user and Selenium webdriver can enter the text using sendkeys(” input…. “).

3) Confirmation Alert

This confirmation alert asks permission to do some type of operation.

How to handle Alert in Selenium WebDriver

Alert interface provides the below few methods which are widely used in Selenium Webdriver.

driver.switchTo().alert().dismiss(); driver.switchTo().alert().accept();

3) String getText() driver.switchTo().alert().getText();

4) void sendKeys(String stringToSend) driver.switchTo().alert().sendKeys("Text");

You can see a number of Alert methods are displayed as shown in below screen suggested by Eclipse.

We can easily switch to alert from the main window by using Selenium’s .switchTo() method.

Now we automate the given below scenario.

In this scenario, we will use Guru99 demo site to illustrate Selenium Alert handling.

Step 2) Enter Any Customer id.

Step 4) Reject/accept the alert.

Handling Alert in Selenium Webdriver import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.NoAlertPresentException; import org.openqa.selenium.Alert; public class AlertDemo { public static void main(String[] args) throws NoAlertPresentException,InterruptedException { System.setProperty("webdriver.chrome.driver","G:\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.findElement(By.name("cusid")).sendKeys("53920"); driver.findElement(By.name("submit")).submit(); Alert alert = driver.switchTo().alert(); String alertMessage= driver.switchTo().alert().getText(); System.out.println(alertMessage); Thread.sleep(5000); alert.accept(); } }

Output:

When you execute the above code, it launches the site. Try to delete Customer ID by handling confirmation alert that displays on the screen, and thereby deleting customer id from the application.

How to handle Pop-up window using Selenium Webdriver

In automation, when we have multiple windows in any web application, the activity may need to switch control among several windows from one to other in order to complete the operation. After completion of the operation, it has to return to the main window i.e. parent window in Selenium. We will see this further in the article with an example.

In Selenium web driver there are methods through which we can handle multiple windows.

Driver.getWindowHandles(); Driver.getWindowHandle();

When the site opens, we need to handle the main window by driver.getWindowHandle(). This will handle the current window that uniquely identifies it within this driver instance. Its return type is String.

For Window handling in Selenium, we will follow the below steps:

Now, we will automate the given below scenario to see how to handle multiple windows using Selenium Webdriver.

In this scenario, we will use “Guru99” demo site to illustrate window handling.

Step 1) Launch the site.

Step 3) New Child Window opens.

A new window opens, ask the user to enter email id and submit the page.

Step 4) Enter your email ID and submit.

Step 5) Display the Access Credentials on submitting the page.

When you execute the code, you will see the child window is open in new tab.

Close the Child window on which credentials are displayed.

    Switch to the parent window.

    How to Handle Multiple Windows in Selenium import java.util.Iterator; import java.util.Set; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class WindowHandle_Demo { public static void main(String[] args) throws InterruptedException { WebDriver driver=new FirefoxDriver(); driver.manage().window().maximize(); String MainWindow=driver.getWindowHandle(); while(i1.hasNext()) { String ChildWindow=i1.next(); if(!MainWindow.equalsIgnoreCase(ChildWindow)) { driver.switchTo().window(ChildWindow); driver.findElement(By.name("emailid")) driver.close(); } } driver.switchTo().window(MainWindow); } }

    Output:

    Multiple Window Handling in Selenium

    Conclusion:

    We defined the types of alert and shown them with a screen shot.

    Demonstrated handling the Alert with Selenium WebDriver using particular scenario.

    Handled multiple windows with Selenium WebDriver using particular scenario.

    You're reading Selenium Alert &Amp; Popup Window Handling: How To Handle?

    Update the detailed information about Selenium Alert &Amp; Popup Window Handling: How To Handle? on the Phuhoabeautyspa.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!