【发布时间】:2014-11-14 17:19:31
【问题描述】:
我正在使用 Eclipse 在 Selenium WebSriver 中创建测试脚本,并且在我有一个父窗口的场景中遇到了障碍,我单击该窗口上的链接并打开一个子窗口。然后我想关闭子窗口并在父窗口上再次执行功能。
我的代码如下:
public static void daysInStockSelectContract(InternetExplorerDriver driver) {
driver.findElement(By.xpath(".//*[@id='page-content']/table/tbody/tr[1]/td[1]/a")).click();
for(String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
driver.close();
}
}
当我运行上面的代码时,子窗口保持打开而父窗口关闭,这与我想要的效果相反。控制台中还会显示如下错误:
“线程“主”org.openqa.selenium.remote.SessionNotFoundException 中的异常:会话 1ff55fb6-71c9-4466-9993-32d7d9cae760 不存在”
我正在为我的 Selenium 脚本使用 IE WebDriver。
更新 - 2014 年 11 月 17 日
Subh,这是我从您发送的链接中使用的代码,不幸的是它似乎不起作用。
public static void daysInStockSelectContract(InternetExplorerDriver driver) {
//get the parent handle before clicking on the link
String winHandleBefore = driver.getWindowHandle();
driver.findElement(By.xpath(".//*[@id='page-content']/table/tbody/tr[1]/td[1]/a")).click();
// the set will contain only the child window now. Switch to child window and close it.
for(String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
}
driver.close();
driver.switchTo().window(winHandleBefore);
}
【问题讨论】:
标签: java eclipse selenium selenium-webdriver