问题
I have a parent window, it has only login fields, i have stored its control using:
String parentWindow= idriver.getWindowHandle();
when the login credentials are entered, a new popup(say "popup A") opens and my app runs in it.I have switched the control to this "popup A" using:
for (String handle1 : idriver.getWindowHandles()) {
idriver.switchTo().window(handle1);
}
Now, when i click a button in this popup A, a popup opens(say "Popup B"), i again used:
for (String handle1 : idriver.getWindowHandles()) {
idriver.switchTo().window(handle1);
}
and the control was transfered to this popup B.
The problem is now i want to swtich to a popup C but the code that worked for switching from parent window to Popup A and then from popup A to popup B is not working.
The control is left on popup B and does not get transfered to window C. Please help. I am using Java, selenium , Win 8, IE 10.
回答1:
I think that in idriver.getWindowHandles()
is your parent window... you need to exclude existed windows.
public String popupHandle(List<String> existingWindowHandles) {
String popupHandle = null;
Set<String> windowHandles = driver.getWindowHandles();
for (String handle : windowHandles) {
for (String existingWindowHandle : existingWindowHandles) {
if (!handle.equals(existingWindowHandle)) {
popupHandle = handle;
break;
}
}
}
return popupHandle;
}
using:
List<String> ignoreWindows = new ArrayList<String>();
String parentWindow = idriver.getWindowHandle();
ignoreWindows.add(parentWindow)
String popUpWindow = popupHandle(ignoreWindows);
idriver.switchTo().window(popUpWindow);
String currentWindow = idriver.getWindowHandle();
ignoreWindows.add(currentWindow);
// doing your code
// new popup appears
popUpWindow = popupHandle(ignoreWindows);
idriver.switchTo().window(popUpWindow);
I think this will help you, good luck. P.S. i didn't try it sorry, i don't have near a hardware to try it.
回答2:
i have used the following and it worked for me:
`String windowTitle = "Popup C";
Set<String> handles = idriver.getWindowHandles();
for (String window : handles) {
Thread.sleep(200);
idriver.switchTo().window(window);
Set<String> mhandles = idriver.getWindowHandles();
for (String mwindow : mhandles) {
idriver.switchTo().window(mwindow);
System.out.println("Im in");
Thread.sleep(200);
if (idriver.getTitle().contains(windowTitle)) {
idriver.switchTo().window(mwindow);
System.out.println(windowTitle);
break;
}
}
}`
if any one has a better suggestion, please feel free to add. thanks
来源:https://stackoverflow.com/questions/20464718/switching-control-between-multiple-child-popups