2013-05-30 78 views
28

這裏我長期以來遇到了一些問題。而我無法弄清楚,有人願意幫助我嗎? ...當我要完成新窗口的任務後切換新窗口。我想關閉新window.and切換舊的窗口,如何在使用Java的Selenium WebDriver中關閉子瀏覽器窗口

所以我在這裏寫的代碼:

// Perform the click operation that opens new window 

String winHandleBefore = driver.getWindowHandle(); 

    // Switch to new window opened 

    for (String winHandle : driver.getWindowHandles()) { 
     driver.switchTo().window(winHandle); 
    } 

    // Perform the actions on new window 


    driver.findElement(By.id("edit-name")).clear(); 
    WebElement userName = driver.findElement(By.id("edit-name")); 
    userName.clear(); 
       try 
    { 
     driver.quit(); 
    } 

    catch(Exception e) 
    { 
     e.printStackTrace(); 
     System.out.println("not close"); 
       } 

driver.switchTo().window(winHandleBefore);// Again I want to start code this old window 

上面我寫的代碼driver.quit()driver.close()。但我得到錯誤。有誰能夠幫助我...?

org.openqa.selenium.remote.SessionNotFoundException:在調用quit()後無法使用FirefoxDriver。

回答

50

要關閉一個瀏覽器窗口:

driver.close(); 

要關閉所有(父母+孩子)的瀏覽器窗口,並結束整個過程:

driver.quit(); 
+0

哇類文件!這是我不知道的技巧 – 8090PZ

4

邏輯你使用將控制切換爲彈出是錯誤的

for (String winHandle : driver.getWindowHandles()) { 
     driver.switchTo().window(winHandle); 
    } 

何上述邏輯將把控制權交給新窗口?


使用下面的邏輯來控制切換到新的窗口

// get all the window handles before the popup window appears 
Set beforePopup = driver.getWindowHandles(); 

// click the link which creates the popup window 
driver.findElement(by).click(); 

// get all the window handles after the popup window appears 
Set afterPopup = driver.getWindowHandles(); 

// remove all the handles from before the popup window appears 
afterPopup.removeAll(beforePopup); 

// there should be only one window handle left 
if(afterPopup.size() == 1) { 
      driver.switchTo().window((String)afterPopup.toArray()[0]); 
} 

// Perform the actions on new window

**`//Close the new window`** 
    driver.close(); 

//perform remain operations in main window

//close entire webDriver session 
    driver.quit(); 
3
//store instance of main window first using below code 
String winHandleBefore = driver.getWindowHandle(); 

執行打開新窗口

//Switch to new window opened 
for (String winHandle : driver.getWindowHandles()) { 
    driver.switchTo().window(winHandle); 
} 

// Perform the actions on new window 
driver.close(); //this will close new opened window 

//switch back to main window using this code 
driver.switchTo().window(winHandleBefore); 

// perform operation then close and quit 
driver.close(); 
driver.quit(); 
0

有()有些情況下一個窗口將一個有效的窗口句柄後關閉本身已經從getWindowHandle獲得或getWindowHandles()的點擊操作。

甚至還有一種可能性,即同時getWindowHandles()正在運行的窗口將關閉本身,除非你創造一些關鍵部分類型的代碼(即凍結瀏覽器在運行測試代碼,直到所有的窗口管理操作完成)

檢查當前驅動程序的有效性的更快方法是檢查由driver.close()或窗口關閉本身使其爲null的sessionId。

的webdriver的需求,以便獲得的sessionId待澆注到遠程驅動程序接口(RemoteWebDriver),如下所示:

if (null == ((RemoteWebDriver)driver).sessionId) { 
    // current window is closed, switch to another or quit 
} else { 
    // current window is open, send commands or close 
} 

還要注意,在關閉最後一個窗口等同於退出()。

0

有2種方式來關閉單個子窗口:

方法1:

driver.close(); 

方式2:通過使用從鍵盤上的Ctrl + W鍵:

driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "w"); 
+0

driver.close();適用於新窗口。 driver.findElement(By.cssSelector(「body」))。sendKeys(Keys.CONTROL +「w」);適用於新窗口和新選項卡。 –

0
public class First { 
    public static void main(String[] args) { 
     System.out.println("Welcome to Selenium"); 
     WebDriver wd= new FirefoxDriver(); 
     wd.manage().window().maximize(); 
     wd.get("http://opensource.demo.orangehrmlive.com/"); 
     wd.findElement(By.id("txtUsername")).sendKeys("Admin"); 
     wd.findElement(By.id("txtPassword")).sendKeys("admin"); 
     wd.findElement(By.id("btnLogin")).submit(); 
     **wd.quit(); //--> this helps to close the web window automatically** 
     System.out.println("Tested Sucessfully "); 
    } 
} 
0

我也試過

1)driver.close();
2)driver.quit();顯然,這些方法不能按預期工作!(並不是說它不工作,雖然)我甚至嘗試過製作驅動程序類singleton,它並沒有幫助我運行測試用例parallel.so它也不是最佳解決方案。最終,我創建了一個單獨的類來運行bat文件。bat文件包含將所有chrome驅動程序進程及其所有子進程分組的命令,以及來自java class I'已經使用Runtime執行了它。

運行該bat文件

public class KillChromeDrivers { 

    public static void main(String args[]) { 


     try { 

      Runtime.getRuntime().exec("cmd /c start E:\\Work_Folder\\SelTools\\KillDrivers.bat"); 
      //Runtime.getRuntime().exec() 
     } catch (Exception ex) { 



     } 
    } 

} 

,你必須把在命令中[蝙蝠]文件

taskkill /IM "chromedriver.exe" /T /F 
相關問題