[英]How can I close a specific window using Selenium WebDriver with Java?
[英]How to close child browser window in Selenium WebDriver using Java
切換到新窗口並完成任務后,我想關閉新窗口並切換到舊窗口,
所以這里我寫的代碼如下:
// 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。
要關閉單個瀏覽器窗口:
driver.close();
要關閉所有(父級+子級)瀏覽器窗口並結束整個會話:
driver.quit();
您用於將控件切換為彈出窗口的邏輯是錯誤的
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();
//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();
在某些情況下,從getWindowHandle()或getWindowHandles()獲取有效的窗口句柄后,窗口將自行關閉。
除非您創建一些關鍵的部分類型代碼(即在運行測試代碼時凍結瀏覽器,直到所有窗口管理操作都完成),否則有可能在getWindowHandles()運行時窗口將自行關閉。
檢查當前驅動程序有效性的一種更快捷的方法是檢查sessionId,它由driver.close()或關閉窗口自身為空。
需要將WebDriver強制轉換為遠程驅動程序接口(RemoteWebDriver)以獲取sessionId,如下所示:
if (null == ((RemoteWebDriver)driver).sessionId) {
// current window is closed, switch to another or quit
} else {
// current window is open, send commands or close
}
另請注意,關閉最后一個窗口等同於quit()。
有兩種方法可以關閉單個子窗口:
方式1:
driver.close();
方式2:使用鍵盤上的Ctrl + w鍵:
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "w");
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 ");
}
}
我也試過了
1)driver.close();
2)driver.quit();
顯然,這些方法不能按預期工作!(不是說它不起作用)我甚至嘗試制作驅動程序類單例,它並沒有幫助我運行並行測試用例。所以它也不是最終解決方案。最后,我想創建一個單獨的類來運行bat文件。bat文件包含對所有chrome驅動程序進程及其所有子進程進行分組的命令。以及我使用java類執行它的java類運行。
運行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) {
}
}
}
您必須放在[.bat]文件中的命令
taskkill /IM "chromedriver.exe" /T /F
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.