我正在使用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:session 1ff55fb6-71c9-4466-9993-32d7d9cae760”不存在“
我正在使用IE WebDriver作为我的Selenium脚本。
更新 - 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);
}
答案 0 :(得分:1)
可能没有正确切换到子窗口。
因此,' driver.close()' 正在关闭父窗口,而不是子窗口。此外,由于父窗口已关闭,这意味着会话已丢失,导致错误' SessionNotFoundException' 。
此链接可以帮助您properly switching between windows
另一方面,只是一个建议。不要将" driver" 作为参数传递,为什么不将它变为静态变量。它可以很容易地被类中的所有方法及其子类访问,并且您不必费心每次都将它传递给方法.. :)
以下是您在评论中提出的代码(与上述问题无关)
public class Testing_anew {
public static WebDriver driver;
public static void main(String args[]) throws InterruptedException{
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
public static void testmethod(){
driver.findElement(By.xpath("//some xpath")).click();
}
更新了代码19/11/14
public static void daysInStockSelectContract(InternetExplorerDriver driver) {
//get the parent handle before clicking on the link
String winHandleBefore = driver.getWindowHandle();
System.out.println("Current handle is: "+winHandleBefore);
driver.findElement(By.xpath(".//*[@id='page-content']/table/tbody/tr[1]/td[1]/a")).click();
// Iterating through the set of window handles till the child window's handle, that is infact
// not equal to the current window handle, is found
for(String winHandle : driver.getWindowHandles()) {
if(!winHandle.equals(winHandleBefore)){
System.out.println("Child handle is: "+ winHandle);
//Sleeping for 4 seconds for detecting Child window
try{
Thread.sleep(4000);
}catch(InterruptedException e){
System.out.println("Caught exception related to sleep:"+e.getMessage());
}
driver.switchTo().window(winHandle);
break;
}
}
System.out.println("AFTER SWITCHING, Handle is: "+driver.getWindowHandle());
driver.close();
//Sleeping for 4 seconds for detecting Parent window
try{
Thread.sleep(4000);
}catch(InterruptedException e){
System.out.println("Caught exception related to sleep:"+e.getMessage());
}
driver.switchTo().window(winHandleBefore); // Switching back to parent window
System.out.println("NOW THE CURRENT Handle is: "+driver.getWindowHandle());
//Now perform some user action here with respect to parent window to assert that the window has switched properly
}
答案 1 :(得分:0)
循环遍历windowHandles时,第一个句柄是父句柄。因此,当您说driver.close()
时,父窗口将关闭。使用以下代码可以避免这种情况:
public static void daysInStockSelectContract(InternetExplorerDriver driver) {
//get the parent handle before clicking on the link
String parentHandle = driver.getWindowHandle();
driver.findElement(By.xpath(".//*[@id='page-content']/table/tbody/tr[1]/td[1]/a")).click();
//get all the window handles and assign it to a set
//It will include child window and parentwindow
Set<String> windowHandles = driver.getWindowHandles();
System.out.println("No of Window Handles: " + windowHandles.size());
//remove the parent handle from the set
windowHandles.remove(parentHandle);
// 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(); } }
如果您将大小设置为1(windowHandles的编号为1)或者获得相同的错误消息,那么该窗口可能是一个警告框。要处理警告框,您可以在单击链接后使用以下代码。
Alert alert = driver.switchTo().alert();
alert.accept();
试用此代码进行调试
public static void daysInStockSelectContract(InternetExplorerDriver driver) {
// get the parent handle before clicking on the link
String parentHandle = driver.getWindowHandle();
System.out.println("ParentHandle : " + parentHandle);
driver.findElement(
By.xpath(".//*[@id='page-content']/table/tbody/tr[1]/td[1]/a"))
.click();
// get all the window handles and assign it to a set
// It will include child window and parentwindow
Set<String> windowHandles = driver.getWindowHandles();
System.out.println("No of Window Handles: " + windowHandles.size());
// remove the parent handle from the set
windowHandles.remove(parentHandle);
// 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);
System.out.println("Child Handle : " + winHandle);
driver.close();
}
// get the window handles again, print the size
windowHandles = driver.getWindowHandles();
System.out.println("No of Window Handles: " + windowHandles.size());
for (String winHandldes : windowHandles) {
System.out.println("Active Window : " + winHandldes);
}
driver.switchTo().window(parentHandle);
}
答案 2 :(得分:0)
我设法通过使用上面Subh概述的步骤解决了这个问题。特别是,确保启用保护模式&#39;已被禁用“安全”功能“互联网选项”选项卡&#39;