• Selenium Video Tutorials

Selenium WebDriver - Chrome 選項



ChromeOptions 是 Selenium WebDriver 中的一個特定類,用於處理僅適用於 Chrome 驅動的選項。它有助於在 Chrome 上執行自動化測試時修改瀏覽器的設定和功能。ChromeOptions 類擴充套件了另一個稱為 MutableCapabilities 的類。

ChromeOptions 類從 Selenium 3.6 版本開始新增。Selenium WebDriver 預設情況下以全新的瀏覽器配置檔案開始,沒有任何預定義的 Cookie、歷史記錄等設定。

使用 ChromeOptions 新增 Chrome 擴充套件程式

讓我們來看一個例子,我們將使用 Selenium IDE 擴充套件程式開啟 Chrome 瀏覽器。Chrome 擴充套件程式應該具有 .crx 檔案。在本例中,我們將獲取 Selenium IDE Chrome 擴充套件程式的 .crx 檔案,並將其放置在測試專案中的 Resources 資料夾下。

Selenium Chrome Options 1

示例

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 ChromeOptnsExtension {
   public static void main(String[] args) throws InterruptedException {

      // object of ChromeOptions
      ChromeOptions opt = new ChromeOptions();

      // adding .crx extension
      opt.addExtensions(new File("./Resources/SeleniumIDE.crx"));

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver(opt);

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // Opening the webpage with Selenium IDE extension
      driver.get("https://tutorialspoint.tw/selenium/practice/register.php");

   }
}

輸出

Process finished with exit code 0
Selenium Chrome Options 2

Chrome 瀏覽器已開啟 Selenium IDE 擴充套件程式,並顯示資訊欄 **Chrome is being controlled by automated test software**。

使用 ChromeOptions 停用資訊欄

在前面的示例中,我們獲得了一個包含文字 **Chrome is being controlled by automated test software** 的資訊欄,但是我們可以使用 ChromeOptions 類停用此資訊欄。

示例

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.Collections;
import java.util.concurrent.TimeUnit;

public class ChromeOptns {
   public static void main(String[] args) throws InterruptedException {

      // object of ChromeOptions
      ChromeOptions opt = new ChromeOptions();

      // adding .crx extensions
      opt.addExtensions(new File("./Resources/SeleniumIDE.crx"));

      // disable information bar
      opt.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver(opt);

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // Opening the webpage with disabling information bar
      driver.get("https://tutorialspoint.tw/selenium/practice/register.php");
   }
}

輸出

Process finished with exit code 0
Selenium Chrome Options 3

Chrome 瀏覽器已啟動 Selenium IDE 擴充套件程式,沒有資訊欄。

使用 ChromeOptions 開啟最大化瀏覽器

在本例中,我們將以最大化模式在 Chrome 瀏覽器中開啟並啟動應用程式。

示例

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.Collections;
import java.util.concurrent.TimeUnit;

public class ChromeOptnsMaximized {
   public static void main(String[] args) throws InterruptedException {

      // object of ChromeOptions
      ChromeOptions opt = new ChromeOptions();

      // adding .crx extensions
      opt.addExtensions(new File("./Resources/SeleniumIDE.crx"));

      // disable information bar
      opt.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));

      // open browser in maximized
      opt.addArguments("--start-maximized");

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver(opt);

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // Opening the webpage with Selenium IDE extension
      driver.get("https://tutorialspoint.tw/selenium/practice/register.php");

      // quitting browser
      driver.quit();
   }
}

輸出

Process finished with exit code 0

在上面的示例中,我們觀察到 Chrome 瀏覽器已啟動 Selenium IDE 擴充套件程式,在最大化瀏覽器中沒有資訊欄 **Chrome is being controlled by automated test software**。

使用 ChromeOptions 處理 SSL 證書

要在 Chrome 中處理 SSL 證書,可以使用 ChromeOptions 類和 DesiredCapabilities 類。要使 DesiredCapabilities 的功能可用於 ChromeOptions,可以使用 merge 方法。

示例

package org.example;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.util.concurrent.TimeUnit;

public class SSLErrorInChrome {
   public static void main(String[] args) throws InterruptedException {
   
      // Desired Capabilities class to profile Chrome
      DesiredCapabilities dc = new DesiredCapabilities();
      dc.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
      
      // Chrome Options class
      ChromeOptions opt = new ChromeOptions();
      
      // merging browser capabilities with options
      opt.merge(dc);
      
      // Initiate the Webdriver with options
      WebDriver driver = new ChromeDriver(opt);
      
      // adding implicit wait of 12 secs
      driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
      
      // launch application 
      driver.get("https://expired.badssl.com");
      
      // obtain browser title
      System.out.println("Browser title in Chrome: " + driver.getTitle());
      
      // quit the browser
      driver.quit();
   }
}

輸出

Browser title in Chrome: Privacy error

結論

本教程全面介紹了 Selenium WebDriver Chrome 選項。我們首先描述了 ChromeOptions 類,然後逐步講解了如何向 Chrome 瀏覽器新增擴充套件程式、如何停用資訊欄、如何最大化瀏覽器以及如何在 Selenium WebDriver 和 ChromeOptions 的幫助下處理 SSL 證書錯誤。這使您能夠深入瞭解 Selenium WebDriver 中的 ChromeOptions 類。明智的做法是繼續練習您所學的內容,並探索與 Selenium 相關的其他內容,以加深您的理解並拓寬您的視野。

廣告