我编写了用于比较日期的代码,并根据时间对对象执行取消操作。我想取消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();
}
}
}
答案 0 :(得分:0)
如果您在selenium中识别任何元素并在以后修改它,则会出现此错误。
在您的情况下,List包含所有元素(EachbookedAppointment),稍后您提到您正在选择取消,我假设此项目被修改或删除。这打破了我们之前找到的列表的完整性,因此您将获得此异常。
<强>提示:强>
get(i)
更改所有get(0)
。因此,您将每次操作列表的第一个元素,这反过来每次都会更改,因为我们在循环中取消它。如果它不起作用,请尝试基于此代码构建自己的代码,在取消对象时共享应用程序的行为(无论是更改还是删除或任何其他属性更改)。获得这些详细信息后,我们将能够帮助您进一步完善代码。
只有您需要记住的是,如果您的WebElement被修改,您将收到此错误。