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
1 . Pop-up window has name/id
2. Pop-up window doesn't have name / you don't want to hard code the window name then go for below logic.
Method-1
Method-2
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.
Method-1
- 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 get newly opened window and switch the control to that (pop-up window handle), as we already know the mainWindowHandle.
Iterator ite = s.iterator();
while(ite.hasNext())
{
String popupHandle=ite.next().toString();
if(!popupHandle.contains(mainWindowHandle))
{
driver.switchTo().window(popupHandle);
}
}
- Now control is in pop-up window, do all your operations in pop-up and close the pop-up window.
- Select the default window again.
driver.switchTo().window( mainWindowHandle );
Method-2
// get all the window handles before the popup window appears SetbeforePopup = driver.getWindowHandles(); // click the link which creates the popup window driver.findElement(by).click(); // get all the window handles after the popup window appears Set afterPopup = driver.getWindowHandles(); // remove all the handles from before the popup window appears afterPopup.removeAll(beforePopup); // there should be only one window handle left if(afterPopup.size() == 1) { driver.switchTo().window((String)afterPopup.toArray()[0]); }
This comment has been removed by the author.
ReplyDelete