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

如何用Selenium WebDriver读取Excel中JSON数据实现应用登录?

我来帮你梳理下问题,你现在的代码有几个关键问题导致无法实现需求,咱们一步步解决:

问题核心分析

你当前代码的最大问题是直接把Excel文件当成JSON文件解析——Excel(.xlsx)是二进制格式,根本不能用FileReader+JSONParser直接读取;另外你Excel里的JSON本身存在语法错误,代码里还出现了变量名冲突的问题,这些都得逐一修复。


步骤1:先修复Excel中的JSON语法错误

你Parameters列里的JSON格式不规范,正确的JSON必须是键值对结构,修复后应该是这样:

{
  "URL": "https://ui.freecrm.com/",
  "usernamexpath": "//input[@id='UserName']",
  "username": "admin",
  "passwordxpath": "//input[@id='Password']",
  "password": "Pass_123",
  "loginButton": "//input[@value='Log in']"
}

⚠️ 注意修正点:

  • 每个键必须用双引号包裹,键和值之间用冒号(不是你写的等于号)
  • 字符串值统一用双引号,不能混用单引号
  • 第一个元素必须是完整的键值对(你之前直接写了URL字符串,没有对应键名)

步骤2:用Apache POI读取Excel文件

要读取Excel内容,必须用专门的Excel操作库,最常用的是Apache POI。先给项目添加依赖(如果用Maven):

<dependencies>
    <!-- 处理XLSX格式的依赖 -->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>5.2.5</version>
    </dependency>
    <!-- 解析JSON的依赖 -->
    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
        <version>1.1.1</version>
    </dependency>
</dependencies>

然后编写读取Excel中Parameters列的工具方法:

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.IOException;

public class ExcelUtils {
    // 读取指定单元格的内容(参数:文件路径、工作表索引、行号、列号,索引都从0开始)
    public static String getCellValue(String filePath, int sheetIndex, int rowNum, int colNum) throws IOException {
        FileInputStream fis = new FileInputStream(filePath);
        Workbook workbook = new XSSFWorkbook(fis);
        Sheet sheet = workbook.getSheetAt(sheetIndex);
        Row row = sheet.getRow(rowNum);
        Cell cell = row.getCell(colNum);
        
        String cellContent = cell.getStringCellValue();
        
        workbook.close();
        fis.close();
        return cellContent;
    }
}

步骤3:解析JSON并提取登录参数

现在可以读取Excel里的JSON字符串,再解析提取参数,同时修复你代码里的变量名冲突问题:

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.IOException;

public class CRMLoginTest {
    public static void main(String[] args) throws IOException, ParseException {
        // 1. 从Excel读取JSON字符串(假设Parameters列是第2列,索引1;数据在第2行,索引1)
        String jsonContent = ExcelUtils.getCellValue("./data/testdata.xlsx", 0, 1, 1);
        
        // 2. 解析JSON字符串
        JSONParser parser = new JSONParser();
        JSONObject loginParams = (JSONObject) parser.parse(jsonContent);
        
        // 3. 提取各个登录参数
        String url = (String) loginParams.get("URL");
        String usernameXpath = (String) loginParams.get("usernamexpath");
        String username = (String) loginParams.get("username");
        String passwordXpath = (String) loginParams.get("passwordxpath");
        String password = (String) loginParams.get("password");
        String loginBtnXpath = (String) loginParams.get("loginButton");
        
        // 验证参数是否正确读取
        System.out.println("读取到的URL:" + url);
        System.out.println("用户名输入框XPath:" + usernameXpath);
        
        // 4. 用Selenium实现登录(示例代码,需添加Selenium依赖)
        // WebDriver driver = new ChromeDriver();
        // driver.get(url);
        // driver.findElement(By.xpath(usernameXpath)).sendKeys(username);
        // driver.findElement(By.xpath(passwordXpath)).sendKeys(password);
        // driver.findElement(By.xpath(loginBtnXpath)).click();
    }
}

额外注意事项

  • 确保Excel文件路径正确,测试阶段可以先用绝对路径避免找不到文件的问题
  • 如果需要处理多行Usecase数据,可以循环读取Excel的行,批量解析参数
  • Selenium部分需要添加对应浏览器驱动的依赖(比如ChromeDriver),并配置好驱动路径

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 06:31:39