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

如何使用Selenium Java验证表格列值并联动校验对应列

问题:如何用Selenium Java校验表格指定行的状态值?

假设表格结构如下(每行包含Col1、Col2、Col3三列):

  • 若第一行Col2包含Part1,需校验该行Col3的值为Release
  • 若第二行Col2包含Part2,需校验该行Col3的值为Release

以下是我尝试实现的代码:

public boolean verifyPartsinEBOM(String partName, String partState) {
    boolean result = true;
    //To locate the table in the EBOM page.
    WebElement mytable = driver.findElement(By.id("tbr"));
    
    //To locate rows of table. 
    List < WebElement > rows_table = mytable.findElements(By.tagName("tr"));
    
    //To calculate no of rows In table.
    int rows_count = rows_table.size();
    
    //Loop will execute till the last row of table.
    for (int row = 0; row < rows_count; row++) {
        
        //To locate columns(cells) of that specific row.
        List < WebElement > columns_row = rows_table.get(row).findElements(By.tagName("td"));
        
        //To calculate no of columns (cells). In that specific row.
        int columns_count = columns_row.size();
        System.out.println("Number of cells In Row " + row + " are " + columns_count);
        
        //Loop will execute till the last cell of that specific row.
        for (int column = 0; column < columns_count; column++) {
            
            // To retrieve text from that specific cell.
            String celtext = columns_row.get(column).getText();
            if(celtext.contains(partName)) {
                WebElement state = driver.findElement(By.xpath(".//*[@id='tbr']/tbody/tr["+Integer.toString(row+1)+"]/td["+Integer.toString(column+3)+"]"));
                String pState = state.getText();
                if(pState.contains(partState)) {
                result = true;
                break;
                }
                else {
                    result = false;
                }
                
                break;
            }
            
            else {
                result = false;
            }               
        }          
    }       
    
    return result;      
}   

原代码存在的问题

  1. 结果覆盖问题:内层循环中,只要当前单元格不匹配partName就直接将result设为false,会覆盖之前找到匹配行时的正确结果。
  2. 元素定位不合理:使用全局driver.findElement定位状态单元格,应该基于当前行元素进行定位,避免页面其他元素干扰。
  3. 列索引计算错误:column+3的写法逻辑混乱,Col3对应的是当前行的第3个单元格(索引为2,因为列表从0开始),无需动态计算。
  4. 未及时终止循环:找到匹配的行并校验后,没有终止外层循环,后续行的遍历可能会修改result的值。

修正后的代码实现

public boolean verifyPartsinEBOM(String partName, String expectedState) {
    // 初始化结果为false,找到匹配且校验通过后再设为true
    boolean result = false;
    // 定位表格
    WebElement table = driver.findElement(By.id("tbr"));
    // 获取所有行
    List<WebElement> rows = table.findElements(By.tagName("tr"));

    for (WebElement row : rows) {
        // 获取当前行的所有单元格
        List<WebElement> cells = row.findElements(By.tagName("td"));
        // 确保当前行有至少3列(Col1、Col2、Col3),避免索引越界
        if (cells.size() < 3) {
            continue;
        }
        // 获取Col2的文本(索引1),去除首尾空格避免干扰
        String col2Text = cells.get(1).getText().trim();
        if (col2Text.contains(partName)) {
            // 获取Col3的文本(索引2)
            String col3Text = cells.get(2).getText().trim();
            // 校验状态是否符合预期
            result = col3Text.equals(expectedState);
            // 找到目标行后直接终止循环,无需继续遍历
            break;
        }
    }
    return result;
}

代码说明

  • 初始化逻辑:默认结果为false,只有找到目标行且状态匹配时才改为true,避免无意义的结果覆盖。
  • 行内定位优化:直接通过当前行的单元格列表获取Col2和Col3,无需使用复杂的XPath,定位更稳定。
  • 边界处理:判断行内单元格数量是否足够,避免索引越界异常。
  • 循环终止:找到目标行后立即break,减少不必要的遍历,提升执行效率。

使用示例

校验第一行Part1的状态:

boolean isPart1Valid = verifyPartsinEBOM("Part1", "Release");

校验第二行Part2的状态:

boolean isPart2Valid = verifyPartsinEBOM("Part2", "Release");

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 08:05:26