
- Selenium 教程
- Selenium - 首頁
- Selenium - 概述
- Selenium - 元件
- Selenium - 自動化測試
- Selenium - 環境設定
- Selenium - 遠端控制
- Selenium IDE 教程
- Selenium - IDE 簡介
- Selenium - 功能
- Selenium - 限制
- Selenium - 安裝
- Selenium - 建立測試
- Selenium - 建立指令碼
- Selenium - 控制流
- Selenium - 儲存變數
- Selenium - 警報和彈出視窗
- Selenium - Selenese 命令
- Selenium - Actions 命令
- Selenium - Accessors 命令
- Selenium - Assertions 命令
- Selenium - Assert/Verify 方法
- Selenium - 定位策略
- Selenium - 指令碼除錯
- Selenium - 驗證點
- Selenium - 模式匹配
- Selenium - JSON 資料檔案
- Selenium - 瀏覽器執行
- Selenium - 使用者擴充套件
- Selenium - 程式碼匯出
- Selenium - 程式碼輸出
- Selenium - JavaScript 函式
- Selenium - 外掛
- Selenium WebDriver 教程
- Selenium - 簡介
- Selenium WebDriver vs RC
- Selenium - 安裝
- Selenium - 第一個測試指令碼
- Selenium - 驅動程式會話
- Selenium - 瀏覽器選項
- Selenium - Chrome 選項
- Selenium - Edge 選項
- Selenium - Firefox 選項
- Selenium - Safari 選項
- Selenium - 雙擊
- Selenium - 右鍵單擊
- Python 中的 HTML 報告
- 處理編輯框
- Selenium - 單個元素
- Selenium - 多個元素
- Selenium Web 元素
- Selenium - 檔案上傳
- Selenium - 定位器策略
- Selenium - 相對定位器
- Selenium - 查詢器
- Selenium - 查詢所有連結
- Selenium - 使用者互動
- Selenium - WebElement 命令
- Selenium - 瀏覽器互動
- Selenium - 瀏覽器命令
- Selenium - 瀏覽器導航
- Selenium - 警報和彈出視窗
- Selenium - 處理表單
- Selenium - 視窗和標籤頁
- Selenium - 處理連結
- Selenium - 輸入框
- Selenium - 單選按鈕
- Selenium - 複選框
- Selenium - 下拉框
- Selenium - 處理 IFrame
- Selenium - 處理 Cookie
- Selenium - 日期時間選擇器
- Selenium - 動態 Web 表格
- Selenium - Actions 類
- Selenium - Action 類
- Selenium - 鍵盤事件
- Selenium - 鍵上/下
- Selenium - 複製和貼上
- Selenium - 處理特殊鍵
- Selenium - 滑鼠事件
- Selenium - 拖放
- Selenium - 筆事件
- Selenium - 滾動操作
- Selenium - 等待策略
- Selenium - 顯式/隱式等待
- Selenium - 支援功能
- Selenium - 多選
- Selenium - 等待支援
- Selenium - 選擇支援
- Selenium - 顏色支援
- Selenium - ThreadGuard
- Selenium - 錯誤和日誌記錄
- Selenium - 異常處理
- Selenium - 其他
- Selenium - 處理 Ajax 呼叫
- Selenium - JSON 資料檔案
- Selenium - CSV 資料檔案
- Selenium - Excel 資料檔案
- Selenium - 跨瀏覽器測試
- Selenium - 多瀏覽器測試
- Selenium - 多視窗測試
- Selenium - JavaScript 執行器
- Selenium - 無頭執行
- Selenium - 捕獲螢幕截圖
- Selenium - 捕獲影片
- Selenium - 頁面物件模型
- Selenium - 頁面工廠
- Selenium - 記錄和回放
- Selenium - 框架
- Selenium - 瀏覽上下文
- Selenium - DevTools
- Selenium Grid 教程
- Selenium - 概述
- Selenium - 架構
- Selenium - 元件
- Selenium - 配置
- Selenium - 建立測試指令碼
- Selenium - 測試執行
- Selenium - 端點
- Selenium - 自定義節點
- Selenium 報告工具
- Selenium - 報告工具
- Selenium - TestNG
- Selenium - JUnit
- Selenium - Allure
- Selenium 和其他技術
- Selenium - Java 教程
- Selenium - Python 教程
- Selenium - C# 教程
- Selenium - Javascript 教程
- Selenium - Kotlin 教程
- Selenium - Ruby 教程
- Selenium - Maven 和 Jenkins
- Selenium - 資料庫測試
- Selenium - LogExpert 日誌記錄
- Selenium - Log4j 日誌記錄
- Selenium - Robot Framework
- Selenium - AutoIT
- Selenium - Flash 測試
- Selenium - Apache Ant
- Selenium - Github 教程
- Selenium - SoapUI
- Selenium - Cucumber
- Selenium - IntelliJ
- Selenium - XPath
Selenium WebDriver - Excel 資料檔案
Selenium Webdriver 可用於與 Excel 資料檔案互動。在自動化測試中,通常需要透過 Excel 檔案為測試用例提供大量資料。這樣做是為了驗證特定場景或建立資料驅動框架。
Java 提供了大量的類和方法,可以使用 Apache POI 庫對 Excel 檔案執行讀寫資料操作。Apache POI API 是一組免費且開源的Java 庫。
什麼是 Apache POI?
Apache POI 用於處理 Microsoft 檔案。它可以用於對各種格式的檔案(包括 Excel)執行讀取、寫入、更新和其他操作。Java 不能直接用於處理 Excel 檔案,因此 Apache POI(第三方 API)與 Java 一起使用,以建立需要從 Excel 中讀取資料的 Selenium 測試。
可以使用 Apache POI 的 Workbook 介面處理 Excel 工作簿。此介面利用 WorkBookFactory 類來生成特定的工作簿。HSSFWorkbook 類(實現 Workbook 介面)具有有助於對具有 .xls 格式的Microsoft Excel 檔案執行讀寫操作的方法。XSSFWorkbook 類(實現 Workbook 介面)具有有助於對具有 .xlsx 或 .xls 格式的 Microsoft Excel 和 OpenOffice XML 檔案執行讀寫操作的方法。
類似地,可以使用 Apache POI 的 Sheet 介面處理 Excel 工作表。HSSFSheet 類(實現 Sheet 介面)具有在 HSSFWorkbook 工作簿(具有 .xls 格式的 Microsoft Excel 檔案)中建立工作表的方法。XSSFSheet 類(實現 Sheet 介面)具有在 XSSFWorkbook 工作簿(具有 .xlsx 或 .xls 格式的 Microsoft Excel 和 OpenOffice XML 檔案)中建立工作表的方法。
可以使用 Apache POI 的 Row 介面處理 Excel 行。HSSFRow 類(實現 Row 介面)具有表示 HSSFSheet 中行的的方法。XSSFRow 類(實現 Row 介面)具有表示 XSSFSheet 中行的的方法。
可以使用 Apache POI 的 Cell 介面處理 Excel 單元格。HSSFCell 類(實現 Row 介面)具有處理 HSSFRow 中單元格的方法。XSSFCell 類(實現 Row 介面)具有處理 XSSFRow 中單元格的方法。
如何安裝 Apache POI?
步驟 1 - 從連結 Apache POI Common 將 Apache POI Common 依賴項新增到 pom.xml 檔案。
步驟 2 - 從連結 Apache POI API Based 將基於 OPC 和 OOXML 架構的 Apache POI API 依賴項新增到 pom.xml 檔案。
步驟 3 - 儲存包含所有依賴項的 pom.xml 並更新Maven 專案。
讀取 Excel 中的所有值
讓我們以名為 Details.xlsx 的 Excel 檔案為例,我們將讀取整個 Excel 檔案並檢索其所有值。

請注意:Details.xlsx 檔案放置在專案下的 Resources 資料夾中,如下圖所示。

示例
package org.example; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class ExcelRead { public static void main(String args[]) throws IOException { // identify location of .xlsx file File f = new File("./Resources/Details.xlsx"); FileInputStream i = new FileInputStream(f); // instance of XSSFWorkbook XSSFWorkbook w = new XSSFWorkbook(i); // create sheet in XSSFWorkbook with name Details1 XSSFSheet s = w .getSheet("Details1"); // handle total rows in XSSFSheet int r = s.getLastRowNum() - s.getFirstRowNum(); // loop through rows for(int k = 0; k <= r ; k++){ // get cells in each row int c = s.getRow(k).getLastCellNum(); for(int j = 0; j < c; j++){ // get cell values System.out.println(s.getRow(k).getCell(j).getStringCellValue()); } } } }
新增到 pom.xml 的依賴項。
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>SeleniumJava</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>16</maven.compiler.source> <maven.compiler.target>16</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.11.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>5.2.5</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.2.5</version> </dependency> </dependencies> </project>
輸出
Name Street Ram Street 12 Rohan Street 110 Process finished with exit code 0
在上面的示例中,我們讀取了整個 Excel 檔案,並在控制檯中獲得了其所有值。
最後,收到訊息 Process finished with exit code 0,表示程式碼已成功執行。
讀取和寫入 Excel 中的值
讓我們以名為 DetailsStudent.xlsx 的 Excel 檔案為例,我們將從該 Excel 檔案中讀取值,並將這些資料輸入到下面的註冊頁面中。成功後,我們將文字 Test Case: Pass 寫入單元格(同一行和 E 列)。如果未成功完成,我們將文字 Test Case: Fail 寫入同一單元格。

下圖顯示了我們將從 DetailsStudent.xlsx 檔案中在 Full Name:、Last Name:、Username: 和 Password 欄位中輸入資料的註冊頁面。

請注意 - DetailsStudent.xlsx Excel 檔案放置在專案下的 Resources 資料夾中,如下圖所示。

示例
package org.example; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.concurrent.TimeUnit; public class ExcelReadWrite { public static void main(String args[]) throws IOException { // identify location of .xlsx file File f = new File("./Resources/DetailsStudent.xlsx"); FileInputStream i = new FileInputStream(f); // instance of XSSFWorkbook XSSFWorkbook w = new XSSFWorkbook(i); // create sheet in XSSFWorkbook with name Details1 XSSFSheet s = w .getSheet("Details1"); // handle total rows in XSSFSheet int r = s.getLastRowNum() - s.getFirstRowNum(); // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 15 secs driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); // Opening the webpage where we will identify elements driver.get("https://tutorialspoint.tw/selenium/practice/register.php#"); //Identify elements for registration WebElement fname = driver.findElement(By.xpath("//*[@id='firstname']")); WebElement lname = driver.findElement(By.xpath("//*[@id='lastname']")); WebElement uname = driver.findElement(By.xpath("//*[@id='username']")); WebElement pass = driver.findElement(By.xpath("//*[@id='password']")); WebElement btn = driver.findElement(By.xpath("//*[@id='signupForm']/div[5]/input")); // loop through rows, read and enter values in form for(int j = 1; j <= r; j++) { fname.sendKeys(s.getRow(j).getCell(0).getStringCellValue()); lname.sendKeys(s.getRow(j).getCell(1).getStringCellValue()); uname.sendKeys(s.getRow(j).getCell(2).getStringCellValue()); pass.sendKeys(s.getRow(j).getCell(3).getStringCellValue()); // submit registration form btn.click(); // verify form submitted WebElement fname1 = driver.findElement(By.xpath("//*[@id='firstname']")); String value = fname1.getAttribute("value"); // create cell at Column 4 to write values in excel XSSFCell c = s.getRow(j).createCell(4); // write results in excel if (value.isEmpty()) { c.setCellValue("Test Case: PASS"); } else { c.setCellValue("Test Case: FAIL"); } // complete writing value in excel FileOutputStream o = new FileOutputStream("./Resources/DetailsStudent.xlsx"); w.write(o); } // closing workbook object w.close(); // Quitting browser driver.quit(); } }
新增到 pom.xml 的依賴項。
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>SeleniumJava</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>16</maven.compiler.source> <maven.compiler.target>16</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.11.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>5.2.5</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.2.5</version> </dependency> </dependencies> </project>
輸出
Process finished with exit code 0
在上面的示例中,我們讀取了整個 Excel 檔案,並在第五列的單元格中寫入值 Test Case: Pass。
最後,收到訊息 Process finished with exit code 0,表示程式碼已成功執行。

如上圖所示,測試執行後,在與Excel中註冊資料相同的Excel檔案**DetailsStudent.xlsx**的第5列中寫入**測試用例:透過**。
結論
本教程全面介紹了Selenium Webdriver Excel資料檔案。我們首先介紹了什麼是Apache POI,如何安裝Apache POI,並透過示例演示瞭如何在Selenium Webdriver的幫助下使用Apache POI讀取和寫入Excel中的值。這將使您深入瞭解Selenium Webdriver中的Excel資料檔案。建議您多練習所學內容,並探索與Selenium相關的其他內容,以加深理解並拓寬視野。