You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从表格中获取最新录入日期实现每次自动加10天的自动化功能

场景说明

我有一个页面需要录入日期,每次录入的日期需要在上次保存的日期基础上加10天。保存后新录入的日期可能出现在表格的任意位置,我需要每次都能从表格中取出最新录入的日期,存储后用于下一次加10天的计算。

最初实现逻辑

初始默认新增行在表格末尾,直接取最后一行的日期,代码如下:

// 读取Last Validity Date列的所有行
List<WebElement> Date = driver.findElements(By.xpath("//table//tr/td[contains(@class,'mat-column-lastValidityDate')last()]")); 
String datetext = Date.get(i).getText();

跳转到新建页面代码:

String NewbtElem = AppXPathsConstants.buttonXpath_replace.replace("XXXX", "New");
clickOnButton(driver, NewbtElem);

新建页面日期加10天逻辑:

System.out.println("Date before Addition: "+datetext);

// 指定和输入日期匹配的格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat outputDateFormat = new SimpleDateFormat("MM/dd/yyyy");

Calendar c = Calendar.getInstance();

try{
    // 把读取到的日期赋值给Calendar对象
    c.setTime(sdf.parse(datetext));
}catch(ParseException e){
    e.printStackTrace();
}

// 要增加的天数
int daysToIncrement = 10;
c.add(Calendar.DATE, daysToIncrement);  

// 格式化加完天数的新日期
String newDate = outputDateFormat.format(c.getTime());
System.out.println("Print the latest date:"+newDate);
// 录入新日期
inputEntry(driver, columnInputEntryXpath_replace.replace("XXXX","Last Validity Date"), newDate);

调整后逻辑

改为取表格中最大的日期作为最新日期,代码如下:

List<WebElement> Date = driver.findElements(By.xpath("//table//tr/td[contains(@class,'mat-column-lastValidityDate')]"));
                            
final String DATE_PATTERN = "yyyy-MM-dd";
LocalDate maxDate = getRows()
        .stream()
        .map(
                webElement -> LocalDate.parse(
                        getDateAsText(webElement),
                        DateTimeFormatter.ofPattern("yyyy-MM-dd")
                )
        )
.max(LocalDate::compareTo).get();

LocalDate addedDate = maxDate.plusDays(10);
System.out.println(addedDate.format(DateTimeFormatter.ofPattern(DATE_PATTERN)));

核心疑问

我当前是通过int rows = Date.size()的方式获取行数量,上述代码中的getRows()方法我应该如何实现?


解决方案

getRows()的核心作用是返回用于提取日期的元素集合,结合配套的getDateAsText方法,提供两种常用实现方案:

方案1:直接返回日期列元素(最简实现)

如果不需要处理同一行的其他字段,直接返回你已经通过driver查询到的日期列元素即可:

/**
 * 获取表格中所有Last Validity Date列的单元格元素
 */
public List<WebElement> getRows() {
    return driver.findElements(By.xpath("//table//tr/td[contains(@class,'mat-column-lastValidityDate')]"));
}

/**
 * 从单元格元素中提取日期文本
 */
public String getDateAsText(WebElement element) {
    // 加trim()避免空格、换行符影响日期解析
    return element.getText().trim();
}

方案2:按行查询后提取日期(扩展性更好)

如果后续需要关联处理同一行的其他字段,可以先查询所有行元素,再逐行提取日期列:

/**
 * 获取表格所有行元素
 */
public List<WebElement> getRows() {
    return driver.findElements(By.xpath("//table//tr"));
}

/**
 * 从行元素中提取Last Validity Date列的日期文本
 */
public String getDateAsText(WebElement rowElement) {
    WebElement dateCell = rowElement.findElement(By.xpath("./td[contains(@class,'mat-column-lastValidityDate')]"));
    return dateCell.getText().trim();
}

额外优化建议

你当前的流操作直接调用.get()存在风险,如果表格为空会抛出NoSuchElementException,建议增加判空处理:

Optional<LocalDate> maxDateOpt = getRows()
        .stream()
        .map(webElement -> LocalDate.parse(
                getDateAsText(webElement),
                DateTimeFormatter.ofPattern("yyyy-MM-dd")
        ))
        .max(LocalDate::compareTo);

if (maxDateOpt.isPresent()) {
    LocalDate addedDate = maxDateOpt.get().plusDays(10);
    System.out.println(addedDate.format(DateTimeFormatter.ofPattern(DATE_PATTERN)));
} else {
    // 表格为空的场景处理,比如用当前日期作为初始值
    LocalDate addedDate = LocalDate.now().plusDays(10);
}

内容的提问来源于stack exchange,提问作者Sandy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 04:12:03