
- 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 - 無頭執行
Selenium Webdriver 可用於在任何瀏覽器上無頭執行測試。無頭執行是在瀏覽器不可見的情況下執行測試。
什麼是無頭執行?
現在更傾向於無頭執行,因為測試用例的執行速度更快,因為在此過程中不會呈現網頁。此外,在無頭模式下執行測試時,所需的系統資源更少。在執行測試以從 Web 應用程式中抓取資料時,大多數情況下都首選無頭執行。
此外,如果我們觸發涉及多個瀏覽器的並行執行,如果我們進行無頭執行,則資源利用率將受到限制。
但是,我們無法對測試進行即時除錯,也無法在測試失敗時使用無頭執行模式向開發人員報告問題。
示例 1
讓我們以以下頁面為例,我們將在 Chrome 瀏覽器中以無頭模式啟動具有 URL 的應用程式,Selenium 版本高於 4。可以使用 Selenium Webdriver 中的 ChromeOptions 類實現無頭執行。
然後,獲取其頁面標題:**Selenium 實踐 - 學生登錄檔單**。接下來,我們將在“姓名”標籤旁邊的輸入框中輸入文字**Selenium**。

然後,我們將單擊“登入”按鈕,之後我們將導航到另一個頁面,該頁面的瀏覽器標題為**Selenium 實踐 - 登入**。

程式碼實現
package org.example; import org.openqa.selenium.*; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.chrome.ChromeOptions; public class Headless { public static void main(String[] args) throws InterruptedException { // setting Chrome options to browser in headless mode ChromeOptions opt = new ChromeOptions(); opt.addArguments("--headless=new"); // Initiate the Webdriver WebDriver driver = new ChromeDriver(opt); // adding implicit wait of 20 secs driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); // Opening the webpage driver.get("https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php"); // getting page title System.out.println("Getting the page title: " + driver.getTitle()); // identify edit box then enter text WebElement e = driver.findElement(By.xpath("//*[@id='name']")); e.sendKeys("Selenium"); // get test entered System.out.println("Value entered: " + e.getAttribute("value")); // perform click WebElement b = driver.findElement(By.xpath("//*[@id='collapseTwo']/div/ul/li[2]/a")); b.click(); // getting page title after click System.out.println("Getting the current title after click: " + driver.getTitle()); // 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> </dependencies> </project>
輸出
Getting the page title: Selenium Practice - Student Registration Form Value entered: Selenium Getting the current title after click: Selenium Practice - Login Process finished with exit code 0
在上面的示例中,我們以無頭模式在瀏覽器上啟動了一個 URL,並獲得了瀏覽器頁面標題,並在控制檯中顯示了訊息 - **獲取頁面標題:Selenium 實踐 - 學生登錄檔單**。然後,我們在輸入框中輸入文字 Selenium 並檢索輸入的值,並在控制檯中顯示訊息 - **輸入的值:Selenium**。之後,我們單擊“登入”連結並在導航到下一頁後獲取當前頁面標題,並在控制檯中顯示訊息:**單擊後獲取當前標題:Selenium - Selenium 實踐 - 登入**。
示例 2
讓我們以以下頁面為例,我們將在 Chrome 瀏覽器中以無頭模式啟動一個應用程式並開啟一個 URL,並獲取其頁面標題 - **Selenium 實踐 - 日期選擇器** 這將透過使用 ChromeOptions 類來完成。然後將值 --headless=new 傳遞給 addArguments 方法。

程式碼實現
package org.example; import org.openqa.selenium.*; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.chrome.ChromeOptions; public class HeadlessBrow { public static void main(String[] args) throws InterruptedException { // setting Chrome options to browser in headless mode ChromeOptions opt = new ChromeOptions(); opt.addArguments("--headless=new"); // Initiate the Webdriver WebDriver driver = new ChromeDriver(opt); // adding implicit wait of 20 secs driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); // Opening the webpage driver.get("https://tutorialspoint.tw/selenium/practice/date-picker.php"); // getting page title System.out.println("Getting the page title: " + driver.getTitle()); // Quitting browser driver.quit(); } }
輸出
Getting the page title: Selenium Practice - Date Picker
在上面的示例中,我們以無頭模式在瀏覽器上啟動了一個 URL,並獲得了瀏覽器頁面標題,並在控制檯中顯示了訊息 - **獲取頁面標題:Selenium 實踐 - 日期選擇器**。
結論
這總結了我們關於 Selenium Webdriver 無頭執行教程的全面概述。我們從描述什麼是無頭執行開始,並逐步介紹瞭如何使用 Selenium Webdriver 處理無頭執行的示例。這使您能夠深入瞭解 Selenium Webdriver 無頭執行。明智的做法是不斷練習您學到的知識,並探索與 Selenium 相關的其他內容,以加深您的理解並擴充套件您的視野。