- 从父窗口我将控件传递给子窗口
- 我正在子窗口中执行操作
- 执行后,将从一个子窗口打开另一个窗口(第一个子窗口的子窗口)。
我必须关闭两个子窗口并返回父窗口。
我无法将控件从子窗口切换到父窗口。我已经尝试了下面的代码
String winHandleBefore = _driver.getWindowHandle(); for(String winHandle : _driver.getWindowHandles()){ _driver.switchTo().window(winHandle); } String winHandleAfter = _driver.getWindowHandle();
/在子窗口中执行操作/
driver.close(); _driver.switchTo().window(winHandleBefore);
问问题
69589 次
9 回答
5
使用此代码:
// Get Parent window handle
String winHandleBefore = _driver.getWindowHandle();
for (String winHandle : _driver.getWindowHandles()) {
// Switch to child window
driver.switchTo().window(winHandle);
}
// Do some operation on child window and get child window handle.
String winHandleAfter = driver.getWindowHandle();
//switch to child window of 1st child window.
for(String winChildHandle : _driver.getWindowHandles()) {
// Switch to child window of the 1st child window.
if(!winChildHandle.equals(winHandleBefore)
&& !winChildHandle.equals(winHandleAfter)) {
driver.switchTo().window(winChildHandle);
}
}
// Do some operation on child window of 1st child window.
// to close the child window of 1st child window.
driver.close();
// to close the child window.
driver.close();
// to switch to parent window.
driver.switchto.window(winHandleBefore);
于 2013-01-31T11:18:37.087 回答
0
在 WebDriver 中关闭多个弹出窗口并切换到父窗口
String parent = driver.getWindowHandle();
Set<String> pops=driver.getWindowHandles();
Iterator<String> it = pops.iterator();
while (it.hasNext()) {
String popupHandle=it.next().toString();
if(!popupHandle.contains(parent)){
driver.switchTo().window(popupHandle);
System.out.println("Popu Up Title: "+ driver.switchTo().window(popupHandle).getTitle());
driver.close();
}
}
于 2013-07-15T06:01:45.927 回答
0
像这样试试。
- 关闭第二个子窗口
- 然后有两个窗口主窗口和第一个子窗口
- 尝试 getWindowhandles(); 它应该返回 2 个窗口句柄。
- 从这两个窗口句柄中提取主窗口句柄。
- 使用上面的窗口句柄切换回主窗口。
或者
driver.switchTo().defaultContent();
http://santoshsarmajv.blogspot.in/2012/04/how-to-switch-control-to-pop-up-window.html
于 2013-01-31T11:22:04.643 回答
0
int i = WebDriver.WindowHandles.Count;
if (i > 1)
{
//switch to child window
WebDriver.SwitchTo().Window(WebDriver.WindowHandles[1]);
//switch to parent window
WebDriver.SwitchTo().Window(WebDriver.WindowHandles[0]);
}
于 2013-08-28T22:39:20.297 回答
0
打电话前试试这个
driver.switchTo().window(parentwindow);
你应该打电话
driver.switchto.defaultcontent .
于 2013-07-15T09:05:20.697 回答
0
试试这个切换到差异窗口句柄并返回父窗口
String winHandleBefore = driver.getWindowHandle();
for(String winHandle : driver.getWindowHandles())
{
driver.switchTo().window(winHandle);
}
for (String handle1 : driver.getWindowHandles())
{
driver.switchTo().window(handle1);
}
// 你的代码在这里
driver.switchTo().window(winHandleBefore); //switch to parent window
于 2015-07-10T07:15:23.680 回答
0
String Parent_Window = driver.getWindowHandle();
for (String Child_Window : driver.getWindowHandles())
{
driver.switchTo().window(Child_Window);
//Perform operation on child window
driver.close();
}
//Switching back to Parent Window
driver.switchTo().window(Parent_Window);
于 2017-09-04T06:03:37.487 回答
0
String parentWindow = driver.getWindowHandle();
Set<String> allWindows = driver.getWindowHandles();
for (String window : allWindows) {
if (!window.equals(parentWindow)) {
String childWindow = window;
driver.switchTo().window(childWindow);
driver.getCurrentlUrl(); // Child windows url link
}
}
driver.switchTo().window(parentWindow); // switching control to the parent window
driver.getCurrentlUrl(); // Parent windows url link
driver.close(); //closes the child window
driver.quit(); //quits the driver
于 2021-11-17T22:05:57.203 回答
-1
试试下面的代码..它对我有用......
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.naukri.com/");
//Get the address of main window
String p = driver.getWindowHandle();
//get address of all windows
Set<String> allWH = driver.getWindowHandles();
//get address of all windows and store it in ArrayList
ArrayList<String> allWHCopy = new ArrayList<String>(allWH);
//close the 2nd window
for(int i=0;i<allWHCopy.size();i++)
{
if(i==2)
{
driver.switchTo().window(allWHCopy.get(i));
driver.close();
}
}
//To switch to main window
driver.switchTo().window(p);
//perform action in main window
}
于 2019-05-15T12:10:42.027 回答