2017-04-03 187 views
-1

我需要關閉子窗口並切換回父窗口才能執行某些操作。如何使用硒webdriver從父窗口切換到子窗口

public static void nextTab() throws InterruptedException { 
    Set winSet = UIPage.getBaseWindow().getWindowHandles(); 
    List winList = new ArrayList(winSet); 
    String newTab = winList.get(winList.size() - 1); UIPage.getBaseWindow().switchTo().window(newTab); 
    Thread.sleep(Constant.LONG_TIMEOUT_500); 
} 

public static void prevTab() { 
    Set winSet = UIPage.getBaseWindow().getWindowHandles(); 
    List winList = new ArrayList(winSet); 
    UIPage.getBaseWindow().close(); 
    String oldTab = winList.get(winList.size() - 2); //System.out.println(oldTab); 
    UIPage.getBaseWindow().switchTo().window(oldTab);  
} 
+0

請格式化源代碼 – dpassy

+0

我想下面鏈接可能對這項有益的。 http://stackoverflow.com/questions/14620253/how-to-switch-control-from-child-window-to-parent-window-in-selenium-webdriver?rq=1 – MamathaMacherla

回答

0

//Get the main window handle

String mainWindowHandle = driver.getWindowHandle();

現在執行它打開子窗口像點擊一個鏈接或按鈕操作。

//Switch to child window and close it

for (String childWindowHandle : driver.getWindowHandles()) { 
    //If window handle is not main window handle then close it 
    if(!childWindowHandle.equals(mainWindowHandle)){ 
    driver.switchTo().window(childWindowHandle); 
    // Close child windows 
    driver.close(); 
    } 
} 

//switch back to main window 
driver.switchTo().window(mainWindowHandle); 
+0

這是工作,但它是開放的兩時間都是父母或孩子的窗口。裏面的循環,我只想調用子窗口一次.plz幫助 –

+0

嗨拉姆基肖爾,更新代碼,只關閉子窗口。看看,讓我知道如果這does not工作。 –

+0

嗨Prudhvi,更新的代碼不起作用。如果我運行此代碼所有當前&主窗口是closed.plz幫助... –