If you want to do any operations in pop-up window you need to switch
the control to pop-up window then do all your operations in that and finally
close the pop-up window and again select the default (main ) window.
Here is WebDriver logic to select Pop-up window
driver.switchTo().window("<window name>");
String mainWindowHandle=driver.getWindowHandle();
Open the pop-up (click on element which causes open a new window)
webElement.click();
while(ite.hasNext())
{
String popupHandle=ite.next().toString();
if(!popupHandle.contains(mainWindowHandle))
{
driver.switchTo().window(popupHandle);
}
}
Here is WebDriver logic to select Pop-up window
1. Pop-up window has name/id
driver.switchTo().window("<window name>");
2. Pop-up window doesn't have name / you don't want to hard code the
window name then go for below logic.
Before opening pop-up get the main window handle.String mainWindowHandle=driver.getWindowHandle();
Open the pop-up (click on element which causes open a new window)
webElement.click();
Try to get all available open window handles with below command. (The
below command returns all window handles as Set)
Set s =
driver.getWindowHandles();
From that above set try to get newly opened window and switch the
control to that (pop-up window handle), as we already know the
mainWindowHandle.
Set s =
driver.getWindowHandles();
Iterator ite = s.iterator();while(ite.hasNext())
{
String popupHandle=ite.next().toString();
if(!popupHandle.contains(mainWindowHandle))
{
driver.switchTo().window(popupHandle);
}
}
Brilliant ideas that you have share with us.It is really help me lot and i hope it will help others also.
ReplyDeleteupdate more different ideas with us.
Selenium Training in Chennai
Selenium Course in Chennai
JAVA Training in Chennai
Python Training in Chennai
Big data training in chennai
Digital Marketing Course in Chennai
Selenium Training in Chennai
Selenium Course in Chennai