在缓存中找不到元素,也许页面已经改变,因为它已查找,selenium webdriver

时间:2017-06-10 09:27:12

标签: java excel selenium

Selenium WebDriver:

我有一个excel表,用户具有多种技能,单个用户具有多行技能,我通过在UI中查看主页中的用户名和excelsheet中的名称来动态输入数据,如果匹配的话它在用户界面中点击用户导航到其他页面,其他函数返回用户在Excel工作表中的行数,并逐行获取用户拥有的所有行的数据,并逐个添加到门户中。 现在,当点击主页返回并执行剩余webelements的操作以验证其他用户是否匹配其余用户时,它会抛出staleelement异常。

显示错误:

Element not found in cache perhaps the page has changed after looked.

错误行:

String [] str = webElement.getText().split("\n"); 

我在单击元素之前尝试thread.sleep,我尝试try/catch但没有任何工作,因为我需要在Excel工作表中的其他用户数据与剩余的web元素进行比较,这是用户名和单击下一个匹配,逐个单元格逐行输入匹配的用户数据。

请帮助,我在过去4天内尝试这个,并且无法弄清楚如何继续让其他用户数据进入门户网站。

public static void main(String[] args) throws Exception {
    WebElement NamePath=driver.findElement(By.xpath(ReadPropertyFile.readProperty("ClickNamePath")));
    java.util.List<WebElement> ListPath1 = driver.findElements(By.xpath((ReadPropertyFile.readProperty("ClickNamePath"))));
    Thread.sleep(3000);
    Iterator<WebElement> itWebelement = ListPath1.iterator();

    while(itWebelement.hasNext())
            {
            WebElement webElement = itWebelement.next();
            String [] str = webElement.getText().split("\n"); 
            String var= str[0];
            System.out.println(var);

        String FilePath1= "D:\\pcdp10012016_demo.xlsx";
        FileInputStream inputStream = new FileInputStream(new File(FilePath1));
        XSSFWorkbook wb = new XSSFWorkbook(inputStream);
        XSSFSheet sheet1= wb.getSheetAt(0);
        List<EmployeePCDPDTO> pcdpDtoObject=ReadExcelData.ReadData(sheet1, var);

                    if(!pcdpDtoObject.isEmpty()){
        webElement.click();
        Iterator<EmployeePCDPDTO> itEmployeepcdpList = pcdpDtoObject.iterator();

            while(itEmployeepcdpList.hasNext())
            {
            EmployeePCDPDTO employeePCDPDTO = itEmployeepcdpList.next();

            Thread.sleep(3000);
            WebElement Addbtn=driver.findElement(By.xpath(ReadPropertyFile.readProperty("AddSkillPath")));
                             Addbtn.click();
                            Thread.sleep(3000);     

                            //select SkillType
                            WebElement s= driver.findElement(By.xpath(".//*[@id='skillTypeDropdown']"));
                            Select oselect= new Select(s);
                            oselect.selectByValue(employeePCDPDTO.getSkillType());
                            Thread.sleep(3000);
                            driver.findElement(By.xpath(".//*[@id='skillName']")).clear();
                             driver.findElement(By.xpath(".//*[@id='skillName']")).sendKeys(employeePCDPDTO.getSkillName());
                            Thread.sleep(3000);
                            //store button
                            driver.findElement(By.xpath(".//*[@id='add-edit-box']/div[7]/div/button[2]")).click();


            }
                    //Home page path in UI
                    Thread.sleep(3000);
                    //home page button
                    driver.findElement(By.xpath(".//*[@id='app']/div/div/div[1]/ol/li[1]/a")).click();
                    Thread.sleep(5000);
        }
                    } 
            }
    }

2 个答案:

答案 0 :(得分:0)

我换了几行。请尝试以下代码并告知我们。

    public static void main(String[] args) throws Exception { 
    WebElement NamePath=driver.findElement(By.xpath(ReadPropertyFile.readProperty("ClickNamePath"))); 
    java.util.List<WebElement> ListPath1 =               driver.findElements(By.xpath((ReadPropertyFile.readProperty("ClickNamePath")))); 
    Thread.sleep(3000);
    // Iterator<WebElement> itWebelement = ListPath1.iterator();
    //while(itWebelement.hasNext())
   for(int i=0; i < ListPath1.size();i++){ 
         WebElement webElement = itWebelement.get(i); 
         String [] str = webElement.getText().split("\n");
         String var= str[0];      
         System.out.println(var); 
         String FilePath1= "D:\\pcdp10012016_demo.xlsx";      
         FileInputStream inputStream = new 
       FileInputStream(new File(FilePath1));    
      XSSFWorkbook wb = new         XSSFWorkbook(inputStream);      
        XSSFSheet sheet1= wb.getSheetAt(0); 
         List<EmployeePCDPDTO> pcdpDtoObject=ReadExcelData.ReadData(sheet1, var);
          if(!pcdpDtoObject.isEmpty()){ webElement.click(); Iterator<EmployeePCDPDTO> itEmployeepcdpList = pcdpDtoObject.iterator(); while(itEmployeepcdpList.hasNext()) { EmployeePCDPDTO employeePCDPDTO = itEmployeepcdpList.next(); Thread.sleep(3000); WebElement Addbtn=driver.findElement(By.xpath(ReadPropertyFile.readProperty("AddSkillPath"))); Addbtn.click(); Thread.sleep(3000); //select SkillType WebElement s= driver.findElement(By.xpath(".//*[@id='skillTypeDropdown']")); Select oselect= new Select(s); oselect.selectByValue(employeePCDPDTO.getSkillType()); Thread.sleep(3000); driver.findElement(By.xpath(".//*[@id='skillName']")).clear(); driver.findElement(By.xpath(".//*[@id='skillName']")).sendKeys(employeePCDPDTO.getSkillName()); Thread.sleep(3000); //store button driver.findElement(By.xpath(".//*[@id='add-edit-box']/div[7]/div/button[2]")).click(); } //Home page path in UI Thread.sleep(3000); //home page button driver.findElement(By.xpath(".//*[@id='app']/div/div/div[1]/ol/li[1]/a")).click(); Thread.sleep(5000); } }ListPath1 =               driver.findElements(By.xpath((ReadPropertyFile.readProperty("ClickNamePath")))); } }

答案 1 :(得分:0)

        for (int i = 0; i < ListPath1.size(); i++) {

            count++;

            if(count>1){

            for(int j=0;j<=2;j++)

            {

                try{

                java.util.List<WebElement> ListPath2 = driver.findElements(By.xpath((ReadPropertyFile.readProperty("ClickNamePath"))));

                webElement = ListPath2.get(count-1);

                String[] str = webElement.getText().split("\n");

                var = str[0];

                System.out.println(var);



                break;

            }

                catch(Exception e)

                {

                    System.out.println(e.getMessage());

                }

            }

            }else{

            webElement = ListPath1.get(i);

            String[] str = webElement.getText().split("\n");

            var = str[0];

            System.out.println(var);

            }