
- 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 - 處理IFrames
- 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 - 討論
使用C#的Selenium教程
Selenium 可以與多種語言一起使用,例如 Java、Python、JavaScript、Ruby、C#等等。Selenium 廣泛用於 Web 自動化測試。Selenium 是一款開源且可移植的自動化軟體測試工具,用於測試 Web 應用程式。
它能夠跨不同的瀏覽器和作業系統執行。Selenium 不僅僅是一個單一的工具,而是一套工具,它幫助測試人員更有效地自動化基於 Web 的應用程式。C# 是 Selenium 的另一種語言繫結。
Selenium 中 Java 和 C# 語言繫結之間沒有主要區別。主要區別僅在於語言本身。此外,這兩種語言的變更日誌也存在差異。
在 Java 中,我們使用 Webdriver、WebElement,但在 C# 中,我們稱之為 IWebdriver、IWebElement。因此,C# 中遵循的約定是在宣告介面時,在介面名稱之前新增字母“I”。
在使用 Java 的頁面工廠中,使用 @FindBy 註解來識別元素,但是,在使用 C# 的頁面工廠中,它被稱為屬性,並用 [FindBy] 表示,用於定位元素。
使用 C# 設定 Selenium 並啟動瀏覽器
步驟 1 - 從以下連結安裝名為 Visual Studio 的 C# 程式碼編輯器:https://visualstudio.microsoft.com/downloads/。
使用此編輯器,我們可以開始在 C# 專案上工作以啟動我們的測試自動化。
要更詳細地瞭解如何設定 Visual Studio,請參閱以下連結:https://tutorialspoint.tw/ebook/。
步驟 2 - 建立一個新專案,方法是右鍵單擊現有專案,然後單擊“新增”選項,然後單擊“新建專案”,或者如果沒有開啟任何專案,請單擊“檔案”選單,然後選擇“新建專案”選項。

步驟 3 - 選擇“控制檯應用程式”選項,然後單擊“下一步”按鈕。

步驟 4 - 輸入專案名稱,例如 SeleniumTest,然後單擊“建立”按鈕。

步驟 5 - 新建立的專案 SeleniumTest 應該可見。

步驟 6 - 右鍵單擊新建立的專案,然後選擇“管理 NuGet 包”選項。

步驟 7 - 在右上角的搜尋框中輸入 selenium,所有基於 selenium 搜尋的包都應該顯示。單擊 Selenium.WebDriver,然後單擊“新增包”按鈕。安裝所有與 Selenium 相關的包。

步驟 8 - 完成後,Selenium.Webdriver 成功新增訊息將在 Visual Studio 的頂部顯示。

步驟 9 - 重新啟動 Visual Studio。再次啟動後,所有安裝的包都應顯示在解決方案資源管理器中的 NuGet 資料夾下。

步驟 10 - 編寫程式碼以獲取以下頁面的頁面標題 - **Selenium Practice - Modal Dialogs**。

示例
using System; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; namespace SeleniumTest { class Program { static void Main(string[] args) { // Initiate Webdriver IWebDriver driver = new ChromeDriver(); // adding an implicit wait of 20 secs driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20); // launch the application driver.Navigate().GoToUrl("https://tutorialspoint.tw/selenium/practice/modal-dialogs.php"); // get the page title String pageTitle = driver.Title; Console.WriteLine("Page title is: " + pageTitle); } } }
輸出
Page title is: Selenium Practice - Modal Dialogs
在上面的示例中,我們在控制檯中啟動了一個應用程式並獲得了其頁面標題,訊息為 - **頁面標題是:Selenium Practice - Modal Dialogs**。
使用 Selenium C# 啟動瀏覽器並退出驅動程式
我們可以使用 driver.Navigate().GoToUrl 方法啟動瀏覽器並開啟應用程式,最後使用 Quit() 方法退出瀏覽器。
using System; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; namespace SeleniumTest { class Program { static void Main(string[] args){ // Initiate Webdriver IWebDriver driver = new ChromeDriver(); // adding an implicit wait of 20 secs driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20); // launch the application driver.Navigate().GoToUrl("https://tutorialspoint.tw/selenium/practice/check-box.php"); // quitting browser driver.Quit(); } } }
在上面的示例中,我們首先在 Chrome 瀏覽器中啟動了一個應用程式,然後退出了瀏覽器。
使用 Selenium C# 識別元素並檢查其功能
導航到網頁後,我們必須與頁面上可用的 Web 元素進行互動,例如單擊連結/按鈕、在編輯框中輸入文字等等,以完成我們的自動化測試用例。
為此,我們的首要任務是識別元素。Selenium 中為此提供了一些定位器,它們是 id、class、類名、name、連結文字、部分連結文字、標籤名、css 和 xpath。這些定位器需要與 FindElement() 方法一起使用。
例如,driver.FindElement(By.Id("id")) 將定位具有給定 id 屬性值的第一個 Web 元素。如果找不到具有匹配 id 屬性值的元素,則應丟擲 NoSuchElementException。
讓我們看看下面圖片中突出顯示的“Impressive”標籤旁邊的單選按鈕的 html 程式碼 -

<input class="form-check-input" type="radio" name="tab" value="igotthree" onclick="show3();">
單擊該單選按鈕後,我們將在網頁上看到文字 **You have checked Impressive**。

示例
using System; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; namespace SeleniumTest { class Program { static void Main(string[] args){ // Initiate the Webdriver IWebDriver driver = new ChromeDriver(); // adding an implicit wait of 20 secs driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20); // launch an application driver.Navigate().GoToUrl("https://tutorialspoint.tw/selenium/practice/radio-button.php"); // identify a radio button then click on radio button IWebElement r = driver.FindElement (By.XPath("/html/body/main/div/div/div[2]/form/div[3]/input")); r.Click(); // identify text after clicking radio button IWebElement txt = driver.FindElement(By.Id("check1")); // obtain text String text = txt.Text; Console.WriteLine("Text is: " + text); // quitting browser driver.Quit(); } } }
輸出
Text is: You have checked Impressive
在上面的示例中,我們在控制檯中獲得了單擊“Impressive”標籤旁邊的單選按鈕後的文字,訊息為 - **You have checked Impressive**。
本教程到此結束,我們全面介紹了 Selenium - C# 教程。我們首先介紹瞭如何使用 C# 設定 Selenium 並啟動瀏覽器,以及如何使用 Selenium C# 識別元素並檢查其功能。
這使您掌握了 Selenium - C# 教程的深入知識。明智的做法是不斷練習您學到的知識,並探索與 Selenium 相關的其他知識,以加深您的理解並拓寬您的視野。