无法使用selenium Webdriver识别naukri.com应用程序的登录按钮

时间:2015-06-06 06:30:55

标签: java selenium-webdriver automation

我正在尝试自动化naukri.com应用程序,作为其中的一部分,当我启动网站时,它基本上显示一些弹出窗口,需要在我继续登录到应用程序之前关闭。这个特定的功能已关闭所有弹出窗口的代码处理,当我继续单击登录按钮时,无法识别登录按钮链接并且脚本失败。如果我注释弹出窗口代码,则会识别登录按钮。请找到相同的以下代码,并帮助我解决问题

   public class naukri {
        WebDriver driver = new FirefoxDriver();

        @Test
        public void pagelaunch() throws InterruptedException{        
          driver.get("http://www.naukri.com");
          driver.manage().window().maximize();
          driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
          String parenthandle = driver.getWindowHandle();
          String parent = driver.getWindowHandle();

          //close all the pop ups
          Set<String> pops=driver.getWindowHandles();
          Iterator<String> it =pops.iterator();
          while (it.hasNext()){    
             String popupHandle=it.next().toString();
             if(!popupHandle.contains(parent))
             {
             driver.switchTo().window(popupHandle);
             System.out.println("Pop Up Title: "+ driver.switchTo().window(popupHandle).getTitle());
             driver.close();
             }
          }

          System.out.println("the system handle is"+parenthandle);

          //to click on login button and proceed to login to the application
          driver.findElement(By.xpath("//a[@title='Jobseeker Login']")).click();
          Thread.sleep(5000);

          for(String winhandle:driver.getWindowHandles())
          {
                driver.switchTo().window(winhandle);
          }

          driver.findElement(By.xpath("//a[@id='uSel']")).click();
          driver.findElement(By.xpath("html/body/div[8]/div[2]/div[2]/form/div[4]/div[2]/input")).sendKeys("anand_qa");
          driver.findElement(By.xpath("html/body/div[8]/div[2]/div[2]/form/div[5]/div[2]/input")).sendKeys("test1234");
          driver.findElement(By.xpath("//div[8]/div[2]/div[2]/form/div[7]/button")).click();
          driver.switchTo().window(parenthandle);
        } 
}

2 个答案:

答案 0 :(得分:1)

 public class naukri {
WebDriver driver = new FirefoxDriver();
    @Test
    public void pageLaunch() throws InterruptedException {
    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.naukri.com");
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    String parenthandle = driver.getWindowHandle();
    String parent = driver.getWindowHandle();

    //close all the pop ups
    Set<String> pops=driver.getWindowHandles();
    {
        Iterator<String> it =pops.iterator();
        while (it.hasNext()) {

            String popupHandle=it.next().toString();
            if(!popupHandle.contains(parent))
            {
                driver.switchTo().window(popupHandle);
                System.out.println("Pop Up Title: "+ driver.switchTo().window(popupHandle).getTitle());
                driver.close();
            }
        }
    }
    System.out.println("the system handle is"+ driver.switchTo().window(parenthandle).getTitle());

    //to click on login button and proceed to login to the application
    driver.findElement(By.xpath("//a[@id='login_Layer']")).click();
    Thread.sleep(5000);
    /*for (String winhandle:driver.getWindowHandles())
    {
        driver.switchTo().window(winhandle);
    }*/

    driver.findElement(By.xpath("//a[@id='uSel']")).click();
    driver.findElement(By.xpath("//form[@id ='lgnFrm']/div[4]/div[2]/input[@id='uLogin']")).sendKeys("anand_qa");
    driver.findElement(By.xpath("//form[@id ='lgnFrm']/div[5]/div[2]/input[@id='pLogin']")).sendKeys("test1234");
    driver.findElement(By.xpath("//form[@id='lgnFrm']/div[7]/button")).click();
}
}

答案 1 :(得分:0)

@ AK17: 您的代码有2个问题

1.关闭弹出窗口后,你没有切换到父手柄,我添加了代码 driver.switchTo()窗口(parenthandle);

2.您的用户名,密码和登录按钮的定位器不正确

工作代码,试试这个:

public class naukri {
        WebDriver driver = new FirefoxDriver();

        @Test
        public void pagelaunch() throws InterruptedException{        
          driver.get("http://www.naukri.com");
          driver.manage().window().maximize();
          driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
          String parenthandle = driver.getWindowHandle();
          String parent = driver.getWindowHandle();

          //close all the pop ups
          Set<String> pops=driver.getWindowHandles();
          Iterator<String> it =pops.iterator();
          while (it.hasNext()){    
             String popupHandle=it.next().toString();
             if(!popupHandle.contains(parent))
             {
             driver.switchTo().window(popupHandle);
             System.out.println("Pop Up Title: "+ driver.switchTo().window(popupHandle).getTitle());
             driver.close();
             }
          }

          System.out.println("the system handle is"+parenthandle);
          driver.switchTo().window(parenthandle);
          WebDriverWait wait = new WebDriverWait(driver,10);
          WebElement login = driver.findElement(By.xpath("//a[@title='Jobseeker Login']"));
          wait.until(ExpectedConditions.elementToBeClickable(login));
          //to click on login button and proceed to login to the application
          driver.findElement(By.xpath("//a[@title='Jobseeker Login']")).click();
          Thread.sleep(3000);

          for(String winhandle:driver.getWindowHandles())
          {
              System.out.println("login: "+winhandle);  
              driver.switchTo().window(winhandle);
          }
          Thread.sleep(3000);   
          driver.findElement(By.xpath("//a[@id='uSel']")).click();
          driver.findElement(By.xpath(".//*[@id='uLogin']")).sendKeys("anand_qa");
          driver.findElement(By.xpath(".//*[@id='pLogin']")).sendKeys("test1234");
          driver.findElement(By.xpath(".//*[@id='lgnFrm']/div[7]/button")).click();
          //driver.switchTo().window(parenthandle);
        } 
}