这里我很久以来遇到了一些问题。我无法弄清楚,有人愿意帮助我吗? ...当我要完成新窗口的任务后,我要切换新窗口。我想关闭那个新窗口。并切换旧窗口,
所以这里我写的代码如下:
// 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。
答案 0 :(得分:52)
关闭单个浏览器窗口:
driver.close();
关闭所有(父级+子级)浏览器窗口并结束整个会话:
driver.quit();
答案 1 :(得分: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();
答案 2 :(得分: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();
答案 3 :(得分:0)
在某些情况下,从getWindowHandle()或getWindowHandles()获取有效的窗口句柄后,窗口将自行关闭。
除非您创建一些关键的部分类型代码(即在运行测试代码时冻结浏览器,直到所有窗口管理操作都完成),否则有可能窗口将在getWindowHandles()运行时自行关闭。 p>
检查当前驱动程序有效性的一种更快捷的方法是检查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()。
答案 4 :(得分:0)
有两种方法可以关闭单个子窗口:
方式1:
driver.close();
方式2:使用键盘上的Ctrl + w键:
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "w");
答案 5 :(得分: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 ");
}
}
答案 6 :(得分:0)
我也试过
1)driver.close();
2)driver.quit();
运行bat文件的类文件
taskkill /IM "chromedriver.exe" /T /F
您必须在[.bat]文件中添加的命令
{{1}}