2015-06-06 65 views
1

我試圖自動化naukri.com應用程序,並且當我啓動該網站時,它基本上顯示了一些需要的彈出窗口集在我繼續登錄到應用程序之前關閉此特定功能。在關閉所有彈出窗口的代碼中處理了此特定功能,當我繼續單擊登錄按鈕時,登錄按鈕鏈接未被識別並且腳本失敗。如果我評論的彈出窗口代碼,然後登錄按鈕是identified.Please找到下面的代碼相同,好心幫我解決這個問題無法識別使用硒的naukri.com應用程序的登錄按鈕Webdriver

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); 
     } 
} 

回答

0

@ AK17: 你代碼有2個問題

1.關閉彈出窗口後,您沒有切換到父主題,我添加了代碼 driver.switchTo()。window(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); 
     } 
} 
+0

driver.switchTo()窗口(parenthandle)。作品,並能夠導航到兒童登錄窗口。感謝您的幫助.. !! – Ak17

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(); 
} 
}