
- Selenium 教程
- Selenium - 首頁
- Selenium - 概述
- Selenium - 元件
- Selenium - 自動化測試
- Selenium - 環境設定
- Selenium - 遠端控制
- Selenium IDE 教程
- Selenium - IDE 簡介
- Selenium - 特性
- Selenium - 限制
- Selenium - 安裝
- Selenium - 建立測試
- Selenium - 建立指令碼
- Selenium - 控制流
- Selenium - 儲存變數
- Selenium - 警報和彈出視窗
- Selenium - Selenese 命令
- Selenium - 操作命令
- Selenium - 訪問器命令
- Selenium - 斷言命令
- 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 從一個新的瀏覽器配置檔案開始,沒有任何關於 Cookie、歷史記錄等的預定義設定。
向瀏覽器新增擴充套件程式
讓我們舉一個例子,我們將使用 **ChromeOptions** 類啟動帶有 Selenium IDE 擴充套件程式的 Chrome 瀏覽器。每個 Chrome 擴充套件程式都應該有一個 .crx 檔案。在這裡,我們將獲取 Selenium IDE Chrome 擴充套件程式的 .crx 檔案,並將其儲存在專案中的 Resources 資料夾中。

示例
package org.example; import org.openqa.selenium.*; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import java.io.File; import java.util.concurrent.TimeUnit; public class ChromeOptns { public static void main(String[] args) throws InterruptedException { // object of ChromeOptions class ChromeOptions opt = new ChromeOptions(); // adding .crx extension opt.addExtensions(new File("./Resources/SeleniumIDE.crx")); // Initiate the Chrome Webdriver WebDriver driver = new ChromeDriver(opt); // adding implicit wait of 10 secs driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Opening the webpage with Selenium IDE extension driver.get("https://tutorialspoint.tw/selenium/practice/register.php"); } }
輸出
Process finished with exit code 0
在這裡,我們觀察到 Chrome 瀏覽器啟動了 Selenium IDE 擴充套件程式,並且資訊欄顯示 **Chrome 正在被自動化測試軟體控制**。

最後,我們收到訊息 **程序已完成,退出程式碼為 0**,表示程式碼成功執行。
以無頭模式開啟瀏覽器
在這個例子中,我們將使用 **EdgeOptions** 類在無頭瀏覽器中啟動一個應用程式。
package org.example; import org.openqa.selenium.*; import org.openqa.selenium.edge.EdgeDriver; import org.openqa.selenium.edge.EdgeOptions; import java.io.File; import java.util.List; import java.util.concurrent.TimeUnit; public class EdgeHeadless { public static void main(String[] args) throws InterruptedException { // object of EdgeOptions FirefoxOptions opt = new EdgeOptions(); // browser in headless mode opt.addArguments("--headless=new"); // Initiate the Webdriver WebDriver driver = new EdgeDriver(opt); // adding implicit wait of 10 secs driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Opening the webpage with headless mode driver.get("https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php"); // getting page title System.out.println("Getting the page title: " + driver.getTitle()); // Quit browser driver.quit(); } }
它將顯示以下輸出:
Getting the page title: Selenium Practice - Student Registration Form
在上面的示例中,我們觀察到 Edge 瀏覽器以 **無頭** 模式啟動。我們還在控制檯中獲得了瀏覽器標題和訊息 - **獲取頁面標題:Selenium 實踐 - 學生登錄檔單**。
在瀏覽器中建立配置檔案
讓我們再舉一個例子,我們將使用 **FirefoxOptions** 類在瀏覽器上擁有 Firefox 配置檔案。
package org.example; import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxOptions; import org.openqa.selenium.firefox.FirefoxProfile; import org.openqa.selenium.firefox.ProfilesIni; import java.util.concurrent.TimeUnit; public class FirefoxProfile { public static void main(String[] args) throws InterruptedException { // object of FirefoxOptions class FirefoxOptions opt = new FirefoxOptions(); // object of ProfilesIni ProfilesIni prof = new ProfilesIni(); // get profile FirefoxProfile p = prof.getProfile("<profileName>"); opt.setProfile(p); // Initiate the Webdriver WebDriver driver = new FirefoxDriver(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"); // obtain page title System.out.println("Obtain the page title: " + driver.getTitle()); // Quit browser driver.quit(); } }
它將顯示以下輸出:
Obtain the page title: Selenium Practice - Student Registration Form
在這裡,Firefox 瀏覽器使用建立的配置檔案開啟。我們還在控制檯中獲得了瀏覽器標題和訊息 - **獲取頁面標題:Selenium 實踐 - 學生登錄檔單**。
處理 Edge 中的 SSL 證書錯誤
為了克服 Edge 瀏覽器中的 SSL 證書錯誤,我們將使用 EdgeOptions 和 DesiredCapabilities 類。
package org.example; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.EdgeDriver; import org.openqa.selenium.firefox.EdgeOptions; import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.DesiredCapabilities; import java.util.concurrent.TimeUnit; public class SSLEdge { public static void main(String[] args) throws InterruptedException { // object of Desired Capabilities class DesiredCapabilities dc = new DesiredCapabilities(); dc.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true); // object Edge Options class EdgeOptionsOptions opt = new EdgeOptions(); opt.merge(dc); // Initiate the Webdriver along with options WebDriver driver = new EdgeDriver(opt); // adding implicit wait of 5 secs driver.manage().timeouts().implicitlyWait(55, TimeUnit.SECONDS); // launch application with SSL certificate error driver.get("https://expired.badssl.com"); // get browser title System.out.println("Get Browser title in Edge: " + driver.getTitle()); // quit the browser driver.quit(); } }
它將顯示以下輸出:
Get Browser title in Edge: Privacy error
在這裡,我們擺脫了 Edge 中的 SSL 證書錯誤並啟動了應用程式。然後在控制檯中獲得了瀏覽器標題和訊息 - **Edge 中的瀏覽器標題:隱私錯誤**。
頁面載入策略
瀏覽器中有多種頁面載入策略可用,分別是 NORMAL(瀏覽器中的預設頁面載入設定)、EAGER 和 NONE。
package org.example; import org.openqa.selenium.*; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; public class ChromePageLoadNormal { public static void main(String[] args) throws InterruptedException { // object of ChromeOptions ChromeOptions opt = new ChromeOptions(); // setting pageloadStrategy to normal opt.setPageLoadStrategy(PageLoadStrategy.NORMAL); // Initiate the Webdriver WebDriver driver = new ChromeDriver(opt); // Opening the webpage driver.get("https://tutorialspoint.tw/selenium/practice/register.php"); // quit browser driver.quit(); } }
它將顯示以下輸出:
Process finished with exit code 0
在上面的示例中,我們設定了瀏覽器選項,使頁面載入策略設定為 NORMAL。如果需要將頁面載入策略設定為 EAGER,則需要進行以下程式碼更改:
// object of ChromeOptions ChromeOptions opt = new ChromeOptions(); // setting pageloadStrategy to EAGER opt.setPageLoadStrategy(PageLoadStrategy.EAGER); // Initiate the Webdriver WebDriver driver = new ChromeDriver(opt);
如果需要將頁面載入策略設定為 NONE,則需要進行以下程式碼更改:
// object of ChromeOptions ChromeOptions opt = new ChromeOptions(); // setting pageloadStrategy to NONE opt.setPageLoadStrategy(PageLoadStrategy.NONE); // Initiate the Webdriver WebDriver driver = new ChromeDriver(opt);
結論
本教程全面介紹了 Selenium Webdriver 瀏覽器選項。我們已經透過示例演示瞭如何向瀏覽器新增配置檔案和擴充套件程式,如何在無頭模式下啟動瀏覽器,如何處理 SSL 證書錯誤以及如何使用瀏覽器選項和 Selenium Webdriver 進行頁面載入策略。這使您能夠深入瞭解 Selenium Webdriver 中的瀏覽器選項。明智的做法是不斷練習您所學到的知識,並探索與 Selenium 相關的其他知識,以加深您的理解並拓寬您的視野。