how to handle popups in selenium webdriver ?

Problem>

How [Pop up handling in Selenium Webdriver using SET and Iterator and switchTo]?

Solution:

popup 2

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.firefox.FirefoxDriver;
public class showModalDialog {
 public static void main(String[] args) {
 WebDriver myTestDriver = new FirefoxDriver();
 myTestDriver.get("http://tinyurl.com/6abw");
 myTestDriver.manage().window().maximize();
 myTestDriver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);

 // Create the set
 Set<String> codeprojectWindowID = myTestDriver.getWindowHandles();
 System.out.println("Main Window Handle ----------------- " + codeprojectWindowID);
 Iterator<String> it = codeprojectWindowID.iterator();

 while (it.hasNext()) {
 // Get element
 Object element = it.next();
 System.out.println(element);
 }

 myTestDriver.findElement(By.xpath("//*[@id='ctl00_Bn']/tbody/tr[1]/td[3]/div/div[3]/span/span/a")).click();
 myTestDriver.findElement(By.xpath("//*[@id='ctl01_MC_MemberLogOn_OpenAuth']/a[2]/img")).click();
 System.out.println("Stand on " + myTestDriver.getTitle() + " Page");

 codeprojectWindowID = myTestDriver.getWindowHandles();
 it = codeprojectWindowID.iterator();
 String MainWindowHandle = it.next();
 String NewWindowHandle = it.next();
 System.out.println(MainWindowHandle);
 System.out.println(NewWindowHandle);

 //Thread.sleep(3000L);

 // go to Google Accounts Window
 myTestDriver.switchTo().window(NewWindowHandle);

 System.out.println("Stand on " + myTestDriver.getTitle() + " Page");
 //Click on Can't access your account?
 myTestDriver.findElement(By.xpath("//*[@id='link-forgot-passwd']")).click();

 // Go to 3rd popup window and click on Submit button - Google Account Recovery

 codeprojectWindowID = myTestDriver.getWindowHandles();

 it = codeprojectWindowID.iterator();
 it.next();
 it.next();

 String NewPopUPWindow = it.next();

 // Go to Google Account Recovery
 myTestDriver.switchTo().window(NewPopUPWindow);
 System.out.println("Stand on " + myTestDriver.getTitle() + " Page");

myTestDriver.findElement(By.xpath("//input[@class='button g-button g-button-submit']")).click();

 System.out.println("Good bye " + myTestDriver.getTitle() + " Page");
 // Close all Opened windows

// close Google Account Recovery
 myTestDriver.close();

 // go to Google Accounts window
 myTestDriver.switchTo().window(NewWindowHandle);
 System.out.println("Good bye " + myTestDriver.getTitle() + " Page");

 // Close Google Accounts Window
 myTestDriver.close();

 // GO to Member Log-on - CodeProject window
 myTestDriver.switchTo().window(MainWindowHandle);
 System.out.println("Good bye " + myTestDriver.getTitle() + " Page");
 //Close Member Log-on - CodeProject window
 myTestDriver.close();

}

}

Get this code from PasteBIN

Console OutPut

Main Window Handle ----------------- [{6997804e-3b34-4b5b-9e91-08247a4594ad}]
{6997804e-3b34-4b5b-9e91-08247a4594ad}
Stand on Member Log-on - CodeProject Page
{6997804e-3b34-4b5b-9e91-08247a4594ad}
{ead76434-1825-491a-a264-43f6ea892f6b}
Stand on Google Accounts Page
Stand on Google Account Recovery Page
Good bye Google Account Recovery Page
Good bye Google Accounts Page
Good bye Member Log-on - CodeProject Page

popup

Advertisements
Pupunzi

For a better web

Tech

News and reviews from the world of gadgets, gear, apps and the web

Digital Inspiration

Tips and tutorials around all things tech.

Thinkwareglobal's Blog

Just another WordPress.com weblog

Kenazari's Blog

Just another WordPress.com weblog

Richfowler1's Blog

Just another WordPress.com weblog

Romaicus's Blog

Just another WordPress.com weblog

botlaguduri

Just another WordPress.com site

chandanag89

Just another WordPress.com site

meghshetty

Just another WordPress.com site

digitalmediaexpert

digital media expert blog

Different Journeys

Let's make life easier!

Thinking in Silverlight

Silverlight/WPF/Windows 8 Store App

LAW RESOURCE INDIA

LEGAL RESOURCE CENTRE / COURT JUDGMENTS / LEGAL ARCHIVES

BookConnect

Promoting Indian language Books

Actively Lazy

Software, crafted with passion

SAPonPower

An ongoing discussion about SAP infrastructure

SAP Career

Your Career Mentor

hrsapcertification

Just another WordPress.com site

The secret product manager

Thoughts about product management, SUSE, Linux, ARM processors, Cloud, AIX, Power Systems, and other things that interest me

Simon Les Flex's Blog

SAP Process Integration Specialist - NOW WE CAN DO BETTER!

Oracle Technologies Primer

An Oracle Fusion Middleware and iPaaS blog!

Gilberto Holms

Java, Middleware, SOA Architecture Blog

The Skeleton

Java code skeletons, tips and pointers

J@n van Zoggel

Journal of a tech junkie

The Pragmatic Integrator

Hints and tips from a pragmatic integration specialist

briskwalk

The pace of life..

Optimalbg's Blog

Just another WordPress.com site

IT Jobs in USA

Keep the track of this Blog if you are looking out for some good Opportunities to work in USA....

Referral Jobs | Right Place to Get a Job

Hurry you are one Click away to get a job - Fresher Jobs for 2012, 2013 fresher’s and also high paid jobs for well Experienced IT professionals on Referal jobs.

Michael Korn's Blog

Musings on Life, Career, Faith and Technology

WORKINGWITHQTP

Just another WordPress.com weblog

Topics

.Net Core, AspNetCore, Entity Framework, Entity Framework Core, AspNetIdentity, AspNetIdentityCore, Unit Test, WCF, Workflows

Expertqtp's Weblog

Just another WordPress.com weblog

Sai Chamarthi

Experienced Testing Professional

saxenavinay

A great WordPress.com site

Suresh4qtp's Blog

Just another WordPress.com site

jonah95hill

This WordPress.com site is the cat’s pajamas

It's always something...

Just another WordPress.com weblog

Faculty of Information Technology - Lê Ngọc Tiến

Be Yourself! Simplify Our Goals! Nothing is Impossible!

Anti-Malware Testing

Thoughts on security product testing past and present

Methods & Tools

Practical knowledge for the software developer, tester and project manager

Mindscripts Software Testing Training Institutes In Pune

Software Testing In Pune, Software Testing Training In Pune, Software Testing Training Institute,Software Testing Training Institutes In Pune ,Software Testing Training Centers,Software Testing Classes In Pune,,Software Testing Courses Pune, Software Testing Certification In Pune,,software testing certification course in pune,software testing certification institutes in pune

edington associates

Working with organizations to develop sustainable and thriving workplaces and people.

Enterprise WordPress hosting, support, and consulting - WordPress VIP

Our best-in-class enterprise WordPress hosting platform, expert consulting and support, and diverse partner ecosystem free you to focus on your business objectives.

WORK Online

Virtual Office at HOME