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

如何存储前页生成的动态ID并在后续待确认页面匹配定位对应记录

解决方案思路

核心逻辑

你只需要在原有流程中先把你操作的Origin对应的动态ID提前存储为变量,跳转至待确认板块后,直接用该变量匹配表格中的ID字段即可定位到对应记录。

代码优化示例

步骤1:原有流程新增存储动态ID的逻辑

修改你现有的遍历表格代码,在找到OriginName为ORIGI的行时,同步提取该行的动态ID值存储:

import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import java.time.Duration;

List<WebElement> originRows = driver.findElements(By.xpath("//table[contains(@class,'mat-table')]//tr"));
String targetOriginId = null;
WebElement targetRow = null;

for(WebElement row : originRows) {
    try {
        // 判断当前行是否为目标Origin行
        row.findElement(By.xpath(".//td[contains(normalize-space(),'ORIGI')]"));
        // 提取该行的动态ID,此处定位器需根据你页面实际元素调整
        // 示例为ID列td包含origin-id类名,若ID是固定第N列,也可写为".//td[N]"
        WebElement idTd = row.findElement(By.xpath(".//td[contains(@class,'origin-id')]"));
        targetOriginId = idTd.getText().trim();
        targetRow = row;
        break;
    } catch (NoSuchElementException e) {
        continue;
    }
}

// 非空判断避免空指针
if(targetRow != null) {
    targetRow.click();
} else {
    throw new RuntimeException("未找到名称为ORIGI的Origin记录");
}

// 原有点击「发送提案」按钮的逻辑保持不变
// ...

步骤2:待确认板块匹配目标记录

跳转至待确认板块后,用之前存储的targetOriginId匹配表格记录:

// 先等待待确认板块表格加载完成,避免页面未渲染导致找不到元素
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement pendingTable = wait.until(ExpectedConditions.visibilityOfElementLocated(
    // 此处定位器需根据你待确认板块表格的实际属性调整
    By.xpath("//div[contains(@class,'pending-confirm')]//table[contains(@class,'mat-table')]")
));

// 直接定位到ID匹配的行
WebElement targetRecordRow = pendingTable.findElement(
    By.xpath(".//tr[.//td[normalize-space()='"+ targetOriginId +"']]")
);

// 后续可对targetRecordRow进行点击、查看详情等操作

注意事项

  • 上述代码中的元素定位器(比如ID列的类名、待确认表格的定位)需要根据你页面的实际DOM结构调整。
  • 若存在同ID多记录的情况,可额外补充提案状态、操作时间等匹配条件,进一步缩小定位范围。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 21:36:00