
- 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 其他概念
- Selenium - IE 驅動程式
- Selenium - 自動化框架
- Selenium - 基於關鍵字的框架
- Selenium - 資料驅動的框架
- Selenium - 混合驅動的框架
- Selenium - SSL 證書錯誤
- Selenium - 替代方案
- Selenium 有用資源
- Selenium - 問答
- Selenium - 快速指南
- Selenium - 有用資源
- Selenium - 自動化實踐
- Selenium - 討論
Selenium Java 教程
Selenium 用於自動化測試 web 應用的測試用例。它支援多種程式語言,例如 Java、Python、C# 等。
如何使用 Java 設定 Selenium?
步驟 1 - 從以下連結下載並安裝 Java:Java 下載。
有關如何設定 Java 的更詳細檢視,請參閱以下連結:Java 環境設定。
成功安裝 Java 後,我們可以透過在命令提示符中執行命令:java 來確認其安裝。
C:\java
步驟 2 - 接下來,我們將透過執行命令:java –version 來確認已安裝的 Java 版本。
java –version
它將顯示以下輸出:
openjdk version "17.0.9" 2023-10-17 OpenJDK Runtime Environment Homebrew (build 17.0.9+0) OpenJDK 64-Bit Server VM Homebrew (build 17.0.9+0, mixed mode, sharing)
步驟 3 - 使用以下連結在我們的系統中安裝 Maven:下載 Apache Maven。
接下來,我們將透過執行以下命令來確認已安裝的Maven版本:
透過執行命令:mvn –version 來確認已安裝的 Maven 版本。
mvn –version.
它將顯示以下輸出:
Apache Maven 3.9.6 (bc0240f3c744dd6b6ec2920b3cd08dcc295161ae) Maven home: /opt/homebrew/Cellar/maven/3.9.6/libexec Java version: 21.0.1, vendor: Homebrew, runtime: /opt/homebrew/Cellar/openjdk/21.0.1/libexec/openjdk.jdk/Contents/Home Default locale: en_IN, platform encoding: UTF-8 OS name: "mac os x", version: "14.0", arch: "aarch64", family: "mac"
執行的命令輸出表明系統中安裝的 Maven 版本為 Apache Maven 3.9.6。
步驟 4 - 安裝名為IntelliJ的程式碼編輯器來編寫和執行 Selenium 測試。
步驟 5 - 在 Main.java 檔案中新增以下程式碼。
package org.example; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class Main { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 12 secs driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS); // URL launch driver.get("https://tutorialspoint.tw/selenium/practice/resizable.php"); // get browser title after browser launch System.out.println("Browser title: " + driver.getTitle()); } }
在 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.19.0</version> </dependency> </dependencies> </project>
步驟 6 - 右鍵單擊並選擇“執行‘Main.main()’”選項。等待執行完成。
步驟 7 - 應該啟動 Chrome 瀏覽器,控制檯中顯示的訊息應為 - 瀏覽器標題:Selenium 實踐 - 可調整大小。
最後,收到訊息程序已完成,退出程式碼為 0,表示程式碼已成功執行。
此外,Chrome 瀏覽器啟動時,頂部會顯示訊息Chrome 受自動化測試軟體控制。
使用 Selenium Java 啟動瀏覽器並退出驅動程式
我們可以使用 driver.get() 方法啟動瀏覽器並開啟應用程式,最後使用 close() 方法關閉瀏覽器。
package org.example; import org.openqa.selenium.WebDriver; import org.openqa.selenium.edge.EdgeDriver; import java.util.concurrent.TimeUnit; public class CloseBrow{ public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 12 secs driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS); // URL launch and get the browser title driver.get("https://tutorialspoint.tw/selenium/practice/droppable.php"); System.out.println( "Browser title obtained : " + driver.getTitle()); // close browser driver.close(); } }
它將顯示以下輸出:
Browser title obtained: Browser Title: Selenium Practice - Droppable Process finished with exit code 0
在上面的示例中,我們首先啟動了 Chrome 瀏覽器,然後獲取了瀏覽器標題,然後關閉了瀏覽器,在控制檯中收到了訊息 - 獲取的瀏覽器標題:Selenium 實踐 - 學生登錄檔單。
如何使用 Selenium Java 識別元素並檢查其功能?
啟動應用程式後,使用者會與頁面上的 web 元素互動,例如單擊連結或按鈕、在輸入框中輸入文字等,以建立自動化測試用例。
第一個任務是找到元素。Selenium 中有多個定位器,即 id、類、類名、名稱、連結文字、部分連結文字、標籤名、css 和 xpath。它們與 Java 中的 findElement() 方法一起使用。
例如,findElement(By.name(“name”)) 將識別具有名稱屬性值的第一個 web 元素。如果名稱屬性值相同的元素為零,則應丟擲 NoSuchElementException
讓我們看看下圖中突出顯示的同一輸入框的 html 程式碼:

<input name="name" id="name" type="text" class="form-control" placeholder="First Name">
上圖中突出顯示的編輯框具有名稱屬性,其值為name。讓我們在識別它之後將文字Selenium輸入到此編輯框中。
package org.example; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class LocatorsName { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 20 secs driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); // Opening the webpage where we will identify edit box enter text driver.get("https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php"); // Identify the search box with name locator to enter text WebElement i = driver.findElement(By.name("name")); i.sendKeys("Selenium"); // Get the value entered String text = i.getAttribute("value"); System.out.println("Entered text is: " + text); // Closing browser driver.quit(); } }
它將顯示以下輸出:
Entered text is: Selenium Process finished with exit code 0
輸出顯示訊息 - 退出程式碼為 0 的程序,這意味著上述程式碼已成功執行。此外,在控制檯中列印了從 getAttribute 方法獲得的編輯框中輸入的值 - Selenium。
結論
本教程總結了 Selenium Java 教程。我們首先介紹瞭如何使用 Java 設定 Selenium,如何使用 Selenium Java 啟動瀏覽器並退出會話,以及如何使用 Selenium Java 識別元素並檢查其功能。這使您掌握了 Selenium Java 教程的深入知識。明智的做法是繼續練習您所學的內容,並探索與 Selenium 相關的其他內容,以加深您的理解並拓寬您的視野。