0
WebDriver driver = new FirefoxDriver();
driver.get("https://www.flipkart.com");
driver.manage().window().maximize();
String parentWindowHandler = driver.getWindowHandle(); // Store your parent window
String subWindowHandler = null;
Set<String> handles = driver.getWindowHandles(); // get all window handles
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()){
    subWindowHandler = iterator.next();
}
driver.switchTo().window(subWindowHandler);

I tried it by switching to main window also. Please add valuable input or code to close the pop up.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Shirsh
  • 73
  • 3
  • 9

2 Answers2

0

The pop-up which appears on Flipkart's website is a simple HTML modal. Window handle is used when a new pop-up window needs to be accessed.

To close the pop-up just click on the cross on the top right corner of the pop-up. Use waits to ensure that selenium finds the WebElement.

Try this:

driver.get("https://www.flipkart.com");
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement cross = wait.until(
    ExpectedConditions.visibilityOfElementLocated(By.className("close-icon")));
cross.click()
JRodDynamite
  • 12,325
  • 5
  • 43
  • 63
0

You can try using the java Robot API by importing java.awt.Robot libraries. An example is here:

One solution for File Upload using Java Robot API with Selenium WebDriver by Java

You can try to use it similarly to press the Esc key. Pressing Esc on flipkart website gets rid of the pop-up.

Community
  • 1
  • 1
Freya
  • 63
  • 1
  • 7
  • Hi Freya, Manually by pressing Esc key it is working. but tried with the below code but not working. Robot robot = new Robot(); robot.keyPress(KeyEvent.VK_ESCAPE); Please correct me if I am wrong. Thanks in Advance. – Shirsh Nov 17 '15 at 17:58
  • Hi Shirsh, you have to release the key too. Try using: Robot robot = new Robot(); robot.keyPress(KeyEvent.VK_ESCAPE); robot.keyRelease(KeyEvent.VK_ESCAPE); – Freya Nov 19 '15 at 00:31
  • Hi Shirsh, please accept my post as an answer if it helped you out. And as always, please let me know if I can be of further help :) – Freya Nov 25 '15 at 02:26