String Parent_Window = driver.getWindowHandle();
for (String Child_Window : driver.getWindowHandles())
{
driver.switchTo().window(Child_Window);
//Perform operation on child window
driver.close();
}
//Switching back to Parent Window
driver.switchTo().window(Parent_Window);
Try like this.
OR
driver.switchTo().defaultContent();
http://santoshsarmajv.blogspot.in/2012/04/how-to-switch-control-to-pop-up-window.html
try the below code.. it worked for me...
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.naukri.com/");
//Get the address of main window
String p = driver.getWindowHandle();
//get address of all windows
Set<String> allWH = driver.getWindowHandles();
//get address of all windows and store it in ArrayList
ArrayList<String> allWHCopy = new ArrayList<String>(allWH);
//close the 2nd window
for(int i=0;i<allWHCopy.size();i++)
{
if(i==2)
{
driver.switchTo().window(allWHCopy.get(i));
driver.close();
}
}
//To switch to main window
driver.switchTo().window(p);
//perform action in main window
}
to close multiple popups in webdriver and switch to parent window
String parent = driver.getWindowHandle();
Set<String> pops=driver.getWindowHandles();
{
Iterator<String> it =pops.iterator();
while (it.hasNext()) {
String popupHandle=it.next().toString();
if(!popupHandle.contains(parent))
{
driver.switchTo().window(popupHandle);
System.out.println("Popu Up Title: "+ driver.switchTo().window(popupHandle).getTitle());
driver.close();
Use this code:
// Get Parent window handle
String winHandleBefore = _driver.getWindowHandle();
for (String winHandle : _driver.getWindowHandles()) {
// Switch to child window
driver.switchTo().window(winHandle);
}
// Do some operation on child window and get child window handle.
String winHandleAfter = driver.getWindowHandle();
//switch to child window of 1st child window.
for(String winChildHandle : _driver.getWindowHandles()) {
// Switch to child window of the 1st child window.
if(!winChildHandle.equals(winHandleBefore)
&& !winChildHandle.equals(winHandleAfter)) {
driver.switchTo().window(winChildHandle);
}
}
// Do some operation on child window of 1st child window.
// to close the child window of 1st child window.
driver.close();
// to close the child window.
driver.close();
// to switch to parent window.
driver.switchto.window(winHandleBefore);
Try this before calling
driver.switchTo().window(parentwindow);
you should call
driver.switchto.defaultcontent .