Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 正在获取以下代码的过时引用异常。我正在使用for循环:-(_Java_Selenium_Selenium Webdriver - Fatal编程技术网

Java 正在获取以下代码的过时引用异常。我正在使用for循环:-(

Java 正在获取以下代码的过时引用异常。我正在使用for循环:-(,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我已经编写了用于比较日期的代码,并根据时间对对象执行了取消操作。我想取消UI上显示的所有对象。但我无法取消所有对象,因为我遇到了过时的引用异常。请帮助 List<WebElement> EachbookedAppointment = driver.findElements(By.xpath(prop.getProperty("eachbookedappointment"))); System.out.println(EachbookedAppointment.size()); f

我已经编写了用于比较日期的代码,并根据时间对对象执行了取消操作。我想取消UI上显示的所有对象。但我无法取消所有对象,因为我遇到了过时的引用异常。请帮助

List<WebElement> EachbookedAppointment = driver.findElements(By.xpath(prop.getProperty("eachbookedappointment")));

System.out.println(EachbookedAppointment.size());

for(int i=0; i< EachbookedAppointment.size();i++)
{
    WebElement AppointmentStatus =EachbookedAppointment.get(i).findElement(By.xpath(prop.getProperty("AppointmentStatus")));
    System.out.println(AppointmentStatus.getText());
    if(AppointmentStatus.getText().equalsIgnoreCase("Scheduled"))
    {
        String bookingTime = EachbookedAppointment.get(i).findElement(By.xpath("//*[@class ='col-sm-10 time-col appPadding']/span")).getText();
        String year = "2016 ";
        StringBuilder sb = new StringBuilder();
        System.out.println(bookingTime);
        sb.append(year).append(bookingTime);
        String UIDate = sb.toString() ;
        System.out.println(UIDate);
        Date date2 = sdfAmerica.parse(UIDate);

        //String appointmentTime = 
        //sdfAmerica.setTimeZone(TimeZone.getTimeZone("America/New_York"));
        Date onedaybackdate = DateUtils.addHours(date, -24);

        System.out.println(date2 + "It is UI date");
        TimeZone.getTimeZone("IST");

        if(date2.before(Currentdate) && date2.after(onedaybackdate)){
            System.out.println(Currentdate + "comes after " + onedaybackdate);
            //EachbookedAppointment.get(i);//.findElement(By.xpath(prop.getProperty("CancelAppointment")));
            WebElement element = (new WebDriverWait(driver, 20)).until(ExpectedConditions.elementToBeClickable(By.xpath(prop.getProperty("CancelAppointment"))));
            //driver.findElement(By.xpath(prop.getProperty("CancelAppointment"))).click();
            element.click();
            waitUntilSpinnerIsDisplayed();
            Assert.assertEquals(driver.findElement(By.xpath(prop.getProperty("AlertMessage"))).getText(), Constant.Cancelwithin24hourMessage);
        }
        else if(date2.after(Currentdate) || date2.before(onedaybackdate))
        {
            System.out.println(date2 + " UI displayed date ");
            EachbookedAppointment.get(i).findElement(By.xpath(prop.getProperty("CancelAppointment"))).click();
            //WebElement element = (new WebDriverWait(driver, 20)).until(ExpectedConditions.elementToBeClickable(By.xpath(prop.getProperty("CancelAppointment"))));
            //driver.findElement(By.xpath(prop.getProperty("CancelAppointment"))).click();
            WebElement CancelYes = (new WebDriverWait(driver, 20)).until(ExpectedConditions.elementToBeClickable(By.xpath(prop.getProperty("CancelYes"))));
            CancelYes.click();
            waitUntilSpinnerIsDisplayed();

            WebElement Alert = (new WebDriverWait(driver, 20)).until(ExpectedConditions.elementToBeClickable(By.xpath(prop.getProperty("AlertMessage"))));
            System.out.println(Alert.getText());
            //Assert.assertEquals(driver.findElement(By.xpath(prop.getProperty("AlertMessage"))).getText(), Constant.CancelAlertMessage);
            waitUntilSpinnerIsDisplayed();
        }
    }
}
List EachbookedAppointment=driver.findElements(By.xpath(prop.getProperty(“EachbookedAppointment”));
System.out.println(eachbookedappoint.size());
for(int i=0;i
如果您识别selenium中的任何元素并在以后对其进行修改,则会出现此错误

在您的例子中,列表包含所有元素(eachbookedappoint),稍后您提到要选择“取消”,我假设此项已被修改或删除。这破坏了我们先前找到的列表的完整性,因此您将获得此异常

提示:

  • 获取列表的大小后,将该大小存储在单独的变量中,并使用它来控制for循环
  • 不要在循环中使用此列表,我们只需要循环的大小
  • 在循环中,第一条语句将再次标识所有对象的列表(EachbookedAppointment),现在在循环中的其他位置使用此列表
  • 参考您的代码,我可以推断您每次都试图取消第一个活动元素,因此,使用
    get(0)
    更改所有
    get(I)
    。因此,您每次都将对列表的第一个元素进行操作,而由于我们在循环中取消它,因此每次都会更改
  • 尝试在此基础上形成您自己的代码,否则如果不起作用,请共享应用程序在取消对象时的行为(无论对象更改、删除或任何其他属性更改)。在我获得这些详细信息后,我将能够进一步帮助您编写代码


    您需要记住的唯一一件事是,如果您的WebElement被修改,您将收到此错误。

    在您单击“取消”后,此对象是否会更改或从列表中删除?发布错误跟踪确认。现在它正在工作