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

Web自动化测试问题:收益数据爬取不全与isSelected验证失败

问题解决方案

任务1:获取所有收益数据并验证降序排列

原代码仅能抓取class为mfFund-nav-row even的行,遗漏了奇数行(class含odd),导致数据不完整。修正方案如下:

修正后的代码

// 显式等待确保元素加载完成,匹配所有数据行
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
List<WebElement> returns = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(
        By.xpath("//tr[contains(@class, 'mfFund-nav-row')]//td[11]")
));

// 提取收益数值并验证降序逻辑
List<Double> returnValues = new ArrayList<>();
for(WebElement returnElement : returns) {
    String returnText = returnElement.getText().replace("%", "").trim();
    try {
        double value = Double.parseDouble(returnText);
        returnValues.add(value);
        System.out.println(value + "%");
    } catch (NumberFormatException e) {
        System.out.println("无法解析收益值:" + returnText);
    }
}

// 验证是否按降序排列
boolean isDescending = true;
for(int i = 1; i < returnValues.size(); i++) {
    if(returnValues.get(i) > returnValues.get(i-1)) {
        isDescending = false;
        break;
    }
}
Assert.assertTrue("收益未按降序排列", isDescending);

核心调整点

  • 调整XPath为//tr[contains(@class, 'mfFund-nav-row')]//td[11],覆盖所有奇偶数据行
  • 加入显式等待,避免因元素未加载导致的抓取遗漏
  • 将文本收益转为数值类型,便于降序逻辑的验证

任务2:验证“Long-Term Historic Returns”是否被选中

原代码直接判断label元素的isSelected()是错误的——实际被选中状态对应的是关联的input复选框,label仅作为点击触发载体。修正方案如下:

修正后的代码

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
// 通过label定位到关联的input复选框
WebElement historicReturnCheckbox = wait.until(ExpectedConditions.visibilityOfElementLocated(
        By.xpath("//label[normalize-space()='Long-Term Historic Returns']/preceding-sibling::input")
));
// 验证复选框的选中状态
Assert.assertTrue("Long-Term Historic Returns未被选中", historicReturnCheckbox.isSelected());

核心调整点

  • 定位对象从label改为关联的input复选框
  • 用显式等待确保元素可交互
  • 调用复选框原生的isSelected()方法判断选中状态

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 06:06:24