2013-01-01 25 views
1

我的网站要求用户登录系统。当用户在输入用户名和密码后点击登录按钮时,主登录窗口会打开一个新的浏览器窗口并在其中显示主页。这在手动执行时绝对正常,但当我尝试使用Webdriver/Java运行类似脚本时,不仅认证失败,而且webdriver会打开2个浏览器弹出窗口而不是一个窗口。我在这里做错了什么?我已经在下面分享了我的代码。使用Internet Explorer驱动程序进行网站验证

我在使用Eclipse IDE的Windows 8和IE 10上使用internetexplorerdriver。

公共类应用{

public static void main(String[] args) { 

    WebDriver driver = new InternetExplorerDriver(); 

    driver.get("http://cmdlhrstg04/QAWorkSpace/datlogin.asp"); 
    driver.findElement(By.id("vchLogin_Name")).sendKeys("xyz"); 
    driver.findElement(By.id("vchPassword")).sendKeys("xx"); 
    driver.findElement(By.id("LoginImg")).click(); 


} 

}

回答

0

,因为有一个其他窗口打开,你将需要处理的事件在新的驱动程序窗口

//Store the current window handle 
     String winHandleBefore = driver.getWindowHandle(); 

     //Perform the click operation that opens new window 
      driver.findElement(By.id("LoginImg")).click(); 

     //Switch to new window opened 
     for(String winHandle : driver.getWindowHandles()){ 
      driver.switchTo().window(winHandle); 
     } 

     // Perform the actions on new window 
     Do your homepage actions 
      //Close the new window, if that window no more required 
    driver.close(); 

     //Switch back to original browser (first window) 

     driver.switchTo().window(winHandleBefore); 


     //continue with original browser (first window) 
     } 
相关问题