
- 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可以使用Color類檢測網頁上web元素的顏色。此外,要檢測顏色、背景顏色和邊框等特性,可以使用getCssValue()方法。
要獲取元素的顏色、背景顏色、邊框顏色,需要將它們作為引數傳遞給getCssValue()方法。它將返回 rgba 格式的值。我們將藉助Color類將rgba值轉換為十六進位制。
識別網頁上的元素
啟動Chrome瀏覽器,並在該瀏覽器上開啟一個應用程式。右鍵單擊網頁,然後單擊“檢查”按鈕。之後,將顯示該頁面的整個HTML程式碼。要識別頁面上的元素,請單擊HTML程式碼頂部左側的向上箭頭,如下所示。

單擊並指向元素(突出顯示的元素)後,其HTML程式碼將出現。此外,顏色和背景顏色資訊將在樣式選項卡中提供。

識別元素的背景和顏色
讓我們以上面頁面上顯示的登入按鈕為例。在“樣式”選項卡中,我們發現其顏色值為#fff,背景顏色為#0d6efd,邊框顏色為#0d6efd。讓我們使用getCssValue()方法獲取該元素的背景顏色和顏色。
示例
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 org.openqa.selenium.support.Color; import java.util.concurrent.TimeUnit; public class ColorSupports { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 15 secs driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); // launching a browser and open a URL driver.get("https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php"); // identify the element WebElement l = driver.findElement(By.xpath("//*[@id='practiceForm']/div[11]/input")); // get element color in rgba format String s = l.getCssValue("color"); System.out.println("rgba code for color: " + s); // convert rgba to hex using Color class String c = Color.fromString(s).asHex(); System.out.println("Hex format for Element Color is: " + c); // get element background color in rgba format String b = l.getCssValue("background-color"); System.out.println("rgba code for background-color: " + b); // convert rgba to hex using Color class String g = Color.fromString(b).asHex(); System.out.println("Hex format Element Background-Color is: " + g); // Closing browser driver.quit(); } }
輸出
rgba code for color: rgba(255, 255, 255, 1) Hex format for Element Color is: #ffffff rgba code for background-color: rgba(13, 110, 253, 1) Hex format Element Background-Color is: #0d6efd Process finished with exit code 0
在上面的例子中,我們以 rgba 格式捕獲了按鈕的顏色,並在控制檯中收到訊息 - 顏色的 rgba 程式碼:rgba(255, 255, 255, 1)。然後將 rgba 格式的顏色轉換為十六進位制格式,並在控制檯中獲得訊息 - 元素顏色的十六進位制格式為:#ffffff。
接下來,我們以 rgba 格式捕獲了同一個按鈕的背景顏色,並在控制檯中收到訊息 - 背景顏色的 rgba 程式碼:rgba(13, 110, 253, 1)。然後將 rgba 格式的背景顏色轉換為十六進位制格式,並在控制檯中獲得訊息 - 元素背景顏色的十六進位制格式為:#0d6efd。
最後,收到訊息程序已完成,退出程式碼為 0,表示程式碼已成功執行。
識別元素的邊框顏色
讓我們再以前面討論的同一個登入按鈕為例,並使用getCssValue()方法獲取邊框顏色。
示例
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 org.openqa.selenium.support.Color; import java.util.concurrent.TimeUnit; public class BorderColorSupports { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 15 secs driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); // launching a browser and open a URL driver.get("https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php"); // identify the element WebElement l = driver.findElement(By.xpath("//*[@id='practiceForm']/div[11]/input")); // get element border color in rgba format String s = l.getCssValue("border-color"); System.out.println("rgba code for border color: " + s); // convert rgba to hex using Color class String g = Color.fromString(s).asHex(); System.out.println("Hex format for element Border-Color is: " + g); // Closing 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>
輸出
rgba code for border color: rgb(13, 110, 253) Hex format for element Border-Color is: #0d6efd
在上面的例子中,我們以 rgba 格式捕獲了按鈕的邊框顏色,並在控制檯中收到訊息 - 顏色的 rgba 程式碼:rgba(13, 110, 253)。然後將 rgba 格式的顏色轉換為十六進位制格式,並在控制檯中獲得訊息 - 元素邊框顏色的十六進位制格式為:#0d6efd。
結論
本教程對 Selenium WebDriver 顏色支援進行了全面講解。我們首先介紹瞭如何在網頁上識別元素,然後透過示例演示瞭如何使用 Selenium WebDriver 識別元素的邊框顏色、背景顏色和顏色。這使您能夠深入瞭解 Selenium WebDriver 顏色支援。最好繼續練習您所學到的知識,並探索與 Selenium 相關的其他內容,以加深您的理解並拓寬您的視野。