PopUp Handling In Selenium Webdriver
Posted By : Nargis Khan | 28-May-2018
Today we will discuss how to handle pop-ups or alert in selenium web driver. I will explain with the example. There is a site called “Disqus” so let start.
Open the browser and enter the https://disqus.com.
After opening the site just click on the “Get Started” link or button. Here a pop-up comes out it ask from us signup in disqus.com. Click on the Google Sign-up button. The First screen is called parent window and Pop-Up which you have seen on the screen that is called child window.
I have to perform some action is the sign in page popup so how to do that. What I have to do on this particular pop-up popup switch from parent to child window then perform some action and close this popup and again switch to the first window. So to handle this type scenario we have window handler API available to selenium so let’s do that.
Here is the code
package testngclasses; import java.util.Iterator; import java.util.Set; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class Popuphandling { public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.chrome.driver", "/home/nargis/Downloads/chromedriver"); WebDriver driver=new ChromeDriver(); //launch browser driver.get("https://disqus.com"); driver.manage().window().maximize(); driver.manage().deleteAllCookies(); //click on the sign up link driver.findElement(By.xpath("//a[text()='Get Started']")).click(); System.out.println("Click on the Get Started Button on the top"); driver.findElement(By.xpath("//button[@title='google']")).click();// click google-signup System.out.println("Click on the Google Button"); Sethandler= driver.getWindowHandles(); Iterator it=handler.iterator(); String ParentwindowId = it.next(); System.out.println("This is the parent Window Id"+ ParentwindowId); String ChildWindowId = it.next(); System.out.println("This is the Child Window Id"+ ChildWindowId); System.out.println("Switch to Child Window"); driver.switchTo().window(ChildWindowId);//go to child window driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); WebElement emailid=driver.findElement(By.name("identifier")); emailid.sendKeys("[email protected]"); System.out.println("Enter Email Id successfully"); driver.findElement(By.xpath("//span[text()='Next']")).click(); driver.findElement(By.name("password")).sendKeys("iamwithyou"); System.out.println("Enter password successfully"); driver.findElement(By.xpath("//span[text()='Next']")).click(); driver.close(); driver.switchTo().window(ParentwindowId);//switch to parent window System.out.println("Success register by Google"); } }
Steps to achieve code
1. Launch the browser.
2. Enter the URL.
3. Maximize the window.
4. Click on the "Get Started" button on the top.
5. Click on the Google button on the signup screen.
6. Child window opened. now you have to switch parent window to child window.
7. Enter the email and click on the "Next" button.
8. Enter the Password and click on the "Next" button
9. close the driver and switch to the parent window.
10. Now you can see the result.
After Run: It launches the browser and site and clicks on the get started button. it opened one signup page and click on the google button it opened the child window where the user enters email and password and return to the parent window.
Result:
Thanks, Hope You like it
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Nargis Khan
Nargis is certified in Manual Testing , she has done B.Tech in computer science. Her Hobbies are reading books & listening music.