有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法识别naukri的登录按钮。使用SeleniumWebDriver的com应用程序

我正在尝试自动化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) 个答案

  1. # 1 楼答案

    @AK17: 你的代码有两个问题

    一,。关闭弹出窗口后,您没有切换到parenthandle,我添加了代码 驾驶员切换到()。窗口(父句柄)

    二,。您的用户名、密码和登录按钮的定位器不正确

    工作代码,请尝试以下操作:

    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);
            } 
    }
    
  2. # 2 楼答案

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