String parentWindowHandler = browser.getWindowHandle(); // Store your parent window
System.out.println(parentWindowHandler);
browser.findElement(By.cssSelector("button.btn-danger")).click();
String subWindowHandler = null;
Set<String> handles = browser.getWindowHandles(); // get all window handles
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()){
subWindowHandler = iterator.next();
}
browser.switchTo().window(subWindowHandler);
System.out.println(subWindowHandler);// switch to popup window
Asked
Active
Viewed 204 times
-1

undetected Selenium
- 183,867
- 41
- 278
- 352

Rahul
- 5
- 2
-
Add some sleep after click `"button.btn-danger"` when debug – yong Feb 11 '18 at 00:54
-
@Rahul, let me know clicking on this `browser.findElement(By.cssSelector("button.btn-danger")).click();` open in new window/tab or in popup? If popup..whether it is in frame? – Sodium Feb 11 '18 at 05:57
-
@GauravGenius.... Thanks. It was a pop-up.It was not a browser tab nor a browser window. I am done with this now as I did not require to store/iterate/switch the window in this case. – Rahul Feb 11 '18 at 06:15
-
@Yong. It did work. Thanks.Problem was like ,While I was finding the elements on my pop-up page loading was not done in full.I did stop my testing method thread to wait for the page load and did work. – Rahul Feb 11 '18 at 06:17
-
@Rahul, Great :) – Sodium Feb 11 '18 at 06:18
1 Answers
-1
Iterator in the top of your collection. When you to iterator.next()
it will be your parent window only. If you want to get child window handle, do on more iterator.next()
while (iterator.hasNext()){
parentWindowHandler = iterator.next();
subWindowHandler = iterator.next();
}

inbha
- 79
- 1
- 9
-
It had use loop to iterator per handle per time, no need to iterator two handles per time. There is no differencen. – yong Feb 11 '18 at 00:52