2013-10-23 133 views
1

我想切換到另一個彈出窗口,但正在給出某種錯誤。我無法弄清楚,爲什麼在不工作的切換窗口,這是給Selenium:無法切換窗口

錯誤代碼是

顯示java.lang.NullPointerException:在進入空值:名稱= NULL

下面是代碼....

String parentWindowHandle = driver.getWindowHandle(); 
     System.out.println("parentWindowHandle......"+parentWindowHandle); 
     //click on "register" 

     Actions act = new Actions(driver); 
      act.click(driver.findElement(By.id("ctl00_ContentPlaceHolder1_btnRegisterMe"))); 
     act.perform(); 

     sleep(26000); 
     String popUpWindowHandle=null; 
     Set<String> openWindowsList = driver.getWindowHandles(); 
     for (String windowHandle : openWindowsList) { 
      System.out.println("windowHandle......"+windowHandle); 
      if (!windowHandle.equals(parentWindowHandle)) 
       {popUpWindowHandle = windowHandle; 

       break;} 
     }             
       // Switching control to newly opened window 

     driver.switchTo().window(popUpWindowHandle); 
+0

您的問題是從'windowHandle.equals(parentWindowHandle)'到來。我的猜測是'parentWindowHandle'沒有被初始化。 – sircapsalot

+0

你好我初始化parentWindowHandle,但忘了把代碼放在這裏。看看我的代碼,我重新發布.... – imtiyaz

回答

0

如果你能夠打印窗口句柄然後嘗試

if (!windowHandle.equalsIgnoreCase(parentWindowHandle)){ 
      popUpWindowHandle = windowHandle; 
      break; 
} 

代替

if(!windowHandle.equals(parentWindowHandle)){} 
+0

thanx 4的幫助....它的工作與if(!windowHandle.equals(parentWindowHandle) ){} – imtiyaz

+0

你可以分享什麼是實際問題,你做了什麼修復? – user861594

+0

我把睡眠時間從5sc增加到了26sc ......實際上問題是窗口切換代碼在打開新的彈出窗口之前執行.... – imtiyaz

相關問題