如何让Selenium自动读取Excel所有行数据并填充网页?
解决Selenium自动读取Excel所有行进行网页注册的问题
你只需要对代码做3处关键修改,就能实现自动读取Excel所有行的功能:
核心修改步骤
替换循环的固定行数限制
把原代码里的for (int i= 1; i<=6; i++)改成for (int i= 1; i<=noOfRows; i++),直接用你已经获取到的noOfRows(Excel最后一行的索引)作为循环终止条件,自动遍历所有数据行。修复
row变量未定义的问题
原代码里直接使用row.getCell()但没有获取当前循环对应的行,需要在循环内部第一行添加:XSSFRow row = sheet.getRow(i);添加空行判断(可选但推荐)
如果Excel里有空行,直接读取会触发空指针异常,建议在获取row后加判断:if(row == null) continue;
修正后的完整代码
public static void main(String[] args) throws IOException, InterruptedException { // 替换为实际的GeckoDriver路径 System.setProperty("webdriver.gecko.driver", "path/to/geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("你的注册页面链接"); // 替换为实际的Excel文件路径 FileInputStream file = new FileInputStream("path/to/your/excel.xlsx"); XSSFWorkbook workbook = new XSSFWorkbook(file); XSSFSheet sheet = workbook.getSheet("SO Reg"); int noOfRows = sheet.getLastRowNum(); System.out.println("Excel中的数据行数:" + noOfRows); int cols = sheet.getRow(1).getLastCellNum(); System.out.println("Excel中的列数:" + cols); // 遍历所有数据行(从第1行开始,假设第0行是表头) for (int i = 1; i <= noOfRows; i++) { XSSFRow row = sheet.getRow(i); // 跳过空行 if(row == null) continue; String SO_Name = row.getCell(0).getStringCellValue(); String Contact_Person = row.getCell(1).getStringCellValue(); String Address_1 = row.getCell(2).getStringCellValue(); String Address_2 = row.getCell(3).getStringCellValue(); String City = row.getCell(4).getStringCellValue(); String State = row.getCell(5).getStringCellValue(); String ZipCode = getCellValueAsString(row.getCell(6)); String Phone_Number = getCellValueAsString(row.getCell(8)); String Username = row.getCell(9).getStringCellValue(); String Email = row.getCell(10).getStringCellValue(); String Re_Type_Email = row.getCell(11).getStringCellValue(); // 注册流程 driver.findElement(By.cssSelector("p.text-white:nth-child(4) > a:nth-child(1)")).click(); Thread.sleep(5000); // 填充表单 driver.findElement(By.id("SOName")).sendKeys(SO_Name); driver.findElement(By.id("ContactPerson")).sendKeys(Contact_Person); driver.findElement(By.id("AddressLine1")).sendKeys(Address_1); driver.findElement(By.id("AddressLine2")).sendKeys(Address_2); driver.findElement(By.id("City")).sendKeys(City); driver.findElement(By.id("State")).sendKeys(State); driver.findElement(By.id("ZipCode")).sendKeys(ZipCode); driver.findElement(By.id("Phone")).sendKeys(Phone_Number); driver.findElement(By.id("UserName")).sendKeys(Username); driver.findElement(By.id("Email")).sendKeys(Email); driver.findElement(By.id("RandText")).sendKeys(Re_Type_Email); driver.findElement(By.id("ConfirmBox")).click(); driver.findElement(By.xpath("/html/body/app-root/app-soregistration/div[2]/div/div/div/div/form[2]/div/div[12]/div/button[1]")).click(); driver.findElement(By.cssSelector(".btn-green-text-black")).click(); // 回到注册入口(如果需要继续下一个注册) driver.findElement(By.cssSelector("p.text-white:nth-child(4) > a:nth-child(1)")).click(); Thread.sleep(5000); } // 释放资源 workbook.close(); file.close(); driver.quit(); } // 辅助方法:处理不同类型的单元格,转成字符串 private static String getCellValueAsString(XSSFCell cell) { if(cell == null) return ""; switch (cell.getCellType()) { case STRING: return cell.getStringCellValue(); case NUMERIC: // 处理数字类型(比如邮编、手机号) return String.valueOf((long)cell.getNumericCellValue()); case BOOLEAN: return String.valueOf(cell.getBooleanCellValue()); default: return ""; } }
额外注意事项
- 单元格类型处理:如果Excel里的邮编、手机号是数字格式,直接用
getStringCellValue()会报错,上面的代码添加了getCellValueAsString辅助方法来处理这种情况。 - 替换硬等待:
Thread.sleep()是硬等待,容易因为网络或页面加载慢导致失败,建议改用显式等待:WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("p.text-white:nth-child(4) > a:nth-child(1)"))).click(); - 资源释放:代码最后添加了
workbook.close()、file.close()和driver.quit(),避免资源泄漏。
内容的提问来源于stack exchange,提问作者Manshi Prabhu
相关产品推荐
相关产品推荐

