- 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 IDE - 定位策略
Selenium IDE 擁有多種定位策略,有助於自動化測試用例。但是,Selenium IDE 預設情況下無法識別網頁上的元素。為了定位元素,我們需要藉助定位器,例如 id、name、css 和 xpath。
除了上述定位器之外,舊版本的 Selenium IDE 還支援其他定位器,例如 DOM 和 identifier,但是它們在最新版本中已被棄用。
示例
讓我們以以下頁面為例,在 First Name: 標籤旁邊輸入文字 Selenium。這可以透過首先使用各種定位器定位元素來實現。
識別 Web 元素
右鍵單擊網頁,然後在 Chrome 瀏覽器中單擊“檢查”按鈕,執行此操作後,網頁的 HTML 程式碼將可訪問。要檢查頁面上的元素,請單擊下面突出顯示的向左向上箭頭。
一旦我們單擊並將箭頭指向輸入框(在下面的影像中突出顯示),它的 HTML 程式碼就會出現。
Selenium IDE 中定位元素的功能
在 Selenium IDE 中錄製後建立測試後,我們就可以透過單擊它來獲取該測試中任何特定步驟的詳細資訊。單擊“目標”欄位中的下拉列表,Selenium IDE 在錄製時成功捕獲了元素的所有定位器,例如 id、name、css、xpath 等。
單擊第 4 步,其 Command、Target 和 Value 欄位的值分別為 type、id=name 和 Selenium。該元素的其他定位器值(如 name、css 等)在“目標”下拉列表中可見。
我們在應用程式上執行的所有步驟都將在 Selenium IDE 的 Command、Target 和 Value 欄位中記錄,右上角有一個 REC 按鈕。
下面突出顯示的影像部分表示文字指令碼編輯器窗格。它包含在啟用錄製時以測試步驟形式記錄的所有使用者互動。
要在 Selenium IDE 中預設識別 Web 元素,請單擊下面影像中突出顯示的 “在頁面中選擇目標” 按鈕。
單擊 “在頁面中選擇目標” 按鈕後,使用者將在網頁上獲得 “選擇元素” 按鈕。選擇元素後,其定位器值將由 Selenium IDE 填充。
所選 Web 元素的對應定位器值已在“目標”中生成。然後,如果單擊 “在頁面中查詢目標” 按鈕,則定位器值已填充的元素將在頁面上突出顯示。
請注意,“在頁面中查詢目標” 和 “在頁面中選擇目標” 按鈕僅在 Command 欄位中存在有效值時才啟用,否則它們將保持停用狀態,如下面的影像中突出顯示的那樣。
定位器 ID
使用 id 定位器定位輸入框,然後在 Selenium IDE 中輸入文字 Selenium 的步驟如下所示:
步驟 1 - 執行儲存和訪問變數的步驟 1 到 4。
步驟 2 - 在 Command 欄位中輸入 open,在 Target 欄位中輸入Selenium 自動化實踐表單,以啟動應用程式。
步驟 3 - 在 Command 欄位中輸入 type,在 Target 欄位中輸入 id=firstname,在 Value 欄位中輸入 Selenium。請注意,id=firstname 是 id 定位器,其值為輸入框的 firstname,Selenium 是要輸入的值。
步驟 4 - 在 Command 欄位中輸入 close 以終止瀏覽器。
步驟 5 - 單擊“執行所有測試”按鈕。
在上面的示例中,我們使用 id 定位器在 First Name: 標籤旁邊的輸入框中輸入了文字 Selenium,該標籤出現在日誌下。此外,我們在頂部獲得了綠色勾號,表示測試已透過。
定位器 Name
使用 name 定位器定位輸入框,然後在 Selenium IDE 中輸入文字 Selenium 的步驟如下所示:
步驟 1 - 單擊上面示例中建立的第二步,並在 Command 欄位中輸入 type,在 Target 欄位中輸入 name=firstname,在 Value 欄位中輸入 Selenium。請注意,name=firstname 是 name 定位器,其值為輸入框的 firstname,Selenium 是要輸入的值。
步驟 2 - 單擊“執行所有測試”,並等待測試執行完成。
在上面的示例中,我們使用 name 定位器在 First Name: 標籤旁邊的輸入框中輸入了文字 Selenium,該標籤出現在日誌下。
定位器 CSS
使用 css 定位器定位輸入框,然後在 Selenium IDE 中輸入文字 Selenium 的步驟如下所示:
步驟 1 - 單擊上面示例中建立的第二步,並在 Command 欄位中輸入 type,在 Target 欄位中輸入 css=input#firstname,在 Value 欄位中輸入 Selenium。請注意,css=input#firstname 是 css 定位器,其值為輸入框的 input#firstname,Selenium 是要輸入的值。
步驟 2 - 單擊“執行所有測試”。
在上面的示例中,我們使用 css 定位器在 First Name: 標籤旁邊的輸入框中輸入了文字 Selenium,該標籤出現在日誌下。
定位器 Xpath
使用 xpath 定位器定位輸入框,然後在 Selenium IDE 中輸入文字 Selenium 的步驟如下所示:
步驟 1 - 單擊上面示例中建立的第二步,並在 Command 欄位中輸入 type,在 Target 欄位中輸入 xpath=//input[@id='firstname'],在 Value 欄位中輸入 Selenium。請注意,xpath=//input[@id='firstname'] 是 xpath 定位器,其值為輸入框的 //input[@id='firstname'],Selenium 是要輸入的值。
步驟 2 - 單擊頂部的“執行所有測試”。
在上面的示例中,我們使用 xpath 定位器在 First Name: 標籤旁邊的輸入框中輸入了文字 Selenium,該標籤出現在日誌下。
結論
本教程全面介紹了 Selenium IDE 定位策略。我們從一個示例開始,逐步講解了不同的定位策略,並演示瞭如何在 Selenium 中使用它們。這將使您深入瞭解 Selenium IDE 中的定位策略。建議您持續練習所學內容,並探索與 Selenium 相關的其他內容,以加深理解並拓寬視野。
