Nan*_*hor 30 java selenium webdriver 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
Run Code Online (Sandbox Code Playgroud)
上面我写了代码driver.quit()或driver.close().但我收到了错误.有谁能够帮我...?
org.openqa.selenium.remote.SessionNotFoundException:调用quit()后无法使用FirefoxDriver.
Mar*_*nds 53
要关闭单个浏览器窗口:
driver.close();
Run Code Online (Sandbox Code Playgroud)
要关闭所有(父级+子级)浏览器窗口并结束整个会话:
driver.quit();
Run Code Online (Sandbox Code Playgroud)
您用于将控件切换到弹出窗口的逻辑是错误的
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
}
Run Code Online (Sandbox Code Playgroud)
上述逻辑如何将控件切换到新窗口?
使用以下逻辑将控件切换到新窗口
// 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]);
}
Run Code Online (Sandbox Code Playgroud)
// Perform the actions on new window
**`//Close the new window`**
driver.close();
Run Code Online (Sandbox Code Playgroud)
//perform remain operations in main window
//close entire webDriver session
driver.quit();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
94150 次 |
| 最近记录: |