
- 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 - 定位策略
啟動應用程式後,使用者會與頁面上的元素進行互動,例如單擊連結/按鈕、在輸入框中鍵入文字、從下拉列表中選擇選項等等,以建立自動化測試用例。第一步是使用其屬性識別元素。可以使用定位器(例如 id、name、class name、xpath、css、tagname、link text 和 partial link)進行此識別。
Id 定位器
元素的 id 屬性可用於識別它。在 Java 中,方法 findElement(By.id("<id 屬性的值>")) 用於查詢具有 id 屬性值的元素。使用此方法,應識別第一個與 id 屬性值匹配的元素。如果不存在 id 屬性值相同的元素,則會丟擲 NoSuchElementException。
語法
Webdriver driver = new ChromeDriver(); driver.findElement(By.id("id value”));
讓我們看一下下面圖片中突出顯示的姓名旁邊的輸入框的 html 程式碼:

上圖中突出顯示的編輯框具有一個 id 屬性,其值為name。讓我們嘗試在此編輯框中輸入文字Selenium。
示例
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 java.util.concurrent.TimeUnit; public class LocatorID { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 20 secs driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); // Opening the webpage where we will identify edit box enter text driver.get("https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php"); // Identify the search box with id locator then enter text Selenium WebElement i = driver.findElement(By.id("name")); i.sendKeys("Selenium"); // Get the value entered String text = i.getAttribute("value"); System.out.println("Entered text is: " + text); // Close browser driver.quit(); } }
它將顯示以下輸出:
Entered text is: Selenium Process finished with exit code 0
輸出顯示訊息 - 程序退出程式碼為 0,表示上述程式碼已成功執行。此外,在控制檯中接收到了在編輯框中輸入的值(從 getAttribute 方法獲得) - Selenium。
Name 定位器
元素的 name 屬性可用於識別它。在 Java 中,方法 findElement(By.name("<name 屬性的值>")) 用於查詢具有 name 屬性值的元素。使用此方法,應識別第一個與 name 屬性值匹配的元素。如果不存在 name 屬性值相同的元素,則會丟擲 NoSuchElementException。
語法
Webdriver driver = new ChromeDriver(); driver.findElement(By.name("name value"));
讓我們研究一下下面圖片中與之前討論的相同輸入框的 html 程式碼:

上圖中突出顯示的編輯框具有一個 name 屬性,其值為name。讓我們在此編輯框中輸入文字Selenium。
示例
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 java.util.concurrent.TimeUnit; public class LocatorName { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 20 secs driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); // Opening the webpage where we will identify edit box enter text driver.get("https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php"); // Identify the search box with name locator to enter text WebElement i = driver.findElement(By.name("name")); i.sendKeys("Selenium"); // Get the value entered String text = i.getAttribute("value"); System.out.println("Entered text is: " + text); // Close browser driver.quit(); } }
它將顯示以下輸出:
Entered text is: Selenium
在控制檯中列印了編輯框中輸入的值 - Selenium。
Class Name 定位器
元素的 class name 屬性可用於識別它。在 Java 中,方法 findElement(By.className("<class name 屬性的值>")) 用於查詢具有 class name 屬性值的元素。使用此方法,應識別第一個與 class name 屬性值匹配的元素。如果不存在 name 屬性值相同的元素,則會丟擲 NoSuchElementException。
語法
Webdriver driver = new ChromeDriver(); driver.findElement(By.className("class name value"));
讓我們看一下下面圖片中突出顯示的點選我的 html 程式碼:

它具有一個 class name 屬性,其值為btn-primary。單擊該元素後,我們將在頁面上獲得您已執行動態點選。

示例
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 java.util.concurrent.TimeUnit; public class LocatorClassName { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 20 secs driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); // Opening the webpage where we will identify button to click driver.get("https://tutorialspoint.tw/selenium/practice/buttons.php"); // Identify button with class name to click WebElement i = driver.findElement(By.className("btn-primary")); i.click(); // Get text after click WebElement e = driver.findElement(By.xpath("//*[@id='welcomeDiv']")); String text = e.getText(); System.out.println("Text is: " + text); // Closing browser driver.quit(); } }
它將顯示以下輸出:
Text is: You have done a dynamic click
我們在單擊點選我按鈕後在控制檯中獲得了文字,訊息為 - 文字為:您已執行動態點選。
TagName 定位器
元素的 tagname 可用於識別它。在 Java 中,方法 findElement(By.tagName("<tagname 的值>")) 用於查詢具有 tagname 值的元素。使用此方法,應識別第一個與 tagname 值匹配的元素。如果不存在 tagname 值相同的元素,則會丟擲 NoSuchElementException。
語法
Webdriver driver = new ChromeDriver(); driver.findElement(By.tagName("tag name value"));
讓我們獲取下面圖片中文字姓名旁邊的輸入框的 html 程式碼:

上圖中突出顯示的輸入框的 tagname 為input。讓我們嘗試在該輸入框中輸入文字Java。
示例
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 java.util.concurrent.TimeUnit; public class LocatorTagName { public static void main(String[] args) throws InterruptedException { //Initiate the Webdriver WebDriver driver = new ChromeDriver(); //adding implicit wait of 10 secs driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // launch a URL driver.get("https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php"); // Identify input box with tagname locator WebElement t = driver.findElement(By.tagName("input")); // then enter text t.sendKeys("Java"); // Get the value entered String text = t.getAttribute("value"); System.out.println("Entered text is: " + text); // Closing browser driver.quit(); } }
它將顯示以下輸出:
Entered text is: Java
在控制檯中列印了編輯框中輸入的值Java。
Link Text 定位器
連結的 link text 可用於識別它。在 Java 中,方法 findElement(By.linkText("<link text 的值>")) 用於查詢具有 link text 值的連結。使用此方法,應識別第一個與 link text 值匹配的元素。如果不存在 link text 值相同的元素,則會丟擲 NoSuchElementException。它主要用於在網頁上定位連結。
語法
Webdriver driver = new ChromeDriver(); driver.findElement(By.linkText("value of link text"));
讓我們看一下下面圖片中突出顯示的連結已建立的 html 程式碼:

它的 link text 值為已建立。單擊它後,我們將在頁面上獲得連結已響應,狀態為 201,狀態文字為已建立。

示例
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 java.util.concurrent.TimeUnit; public class LocatorLinkText { public static void main(String[] args) throws InterruptedException { //Initiate the Webdriver WebDriver driver = new ChromeDriver(); //adding implicit wait of 10 secs driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // launch a URL driver.get("https://tutorialspoint.tw/selenium/practice/links.php"); // Identify link with tagname link text WebElement t = driver.findElement(By.linkText("Created")); // then click t.click(); // Get the text WebElement e = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/div[1]")); String text = e.getText(); System.out.println("Text is: " + text); // Close browser driver.quit(); } }
它將顯示以下輸出:
Text is: Link has responded with status 201 and status text Created
我們在單擊已建立連結後在控制檯中獲得了文字,訊息為 - 連結已響應,狀態為 201,狀態文字為已建立。
Partial Link Text 定位器
連結的 partial link text 可用於識別它。在 Java 中,方法 findElement(By.partialLinkText(“<partial link text 的值>”)) 用於查詢具有 partial link text 值的連結。使用此方法,應識別第一個與 partial link text 值匹配的元素。如果不存在 partial link text 值相同的元素,則會丟擲 NoSuchElementException。它主要用於在網頁上定位連結。
語法
Webdriver driver = new ChromeDriver(); driver.findElement(By.partialLinkText("partial link text value"));
讓我們研究一下下面圖片中突出顯示的連結錯誤請求的 html 程式碼:

它的 link text 值為錯誤請求。單擊它後,我們將在頁面上獲得文字連結已響應,狀態為 400,狀態文字為錯誤請求。

示例
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 java.util.concurrent.TimeUnit; public class LocatorPartialLinkText { public static void main(String[] args) throws InterruptedException { //Initiate the Webdriver WebDriver driver = new ChromeDriver(); //adding implicit wait of 10 secs driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // launch a URL driver.get("https://tutorialspoint.tw/selenium/practice/links.php"); // Identify link with tagname partial link text WebElement t = driver.findElement(By.partialLinkText("Bad")); // then click t.click(); // Get the text WebElement e = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/div[4]")); String text = e.getText(); System.out.println("Text is: " + text); // Closing browser driver.quit(); } }
它將顯示以下輸出:
Text is: Link has responded with status 400 and status text Bad Request
我們在單擊錯誤請求連結後在控制檯中獲得了文字,訊息為 - 連結已響應,狀態為 400,狀態文字為錯誤請求。
CSS 定位器
元素的 css 定位器值可用於識別它。在 Java 中,方法 findElement(By.cssSelector("<css 選擇器的值>")) 用於查詢具有 css 選擇器值的元素。使用此方法,應識別第一個與 css 選擇器值匹配的元素。如果不存在 css 選擇器值相同的元素,則會丟擲 NoSuchElementException。
語法
Webdriver driver = new ChromeDriver(); driver.findElement(By.cssSelector("value of css selector"));
建立 CSS 表示式的規則
下面討論了建立 css 表示式的規則:
- 要使用 css 識別元素,表示式應為 tagname[attribute='value']。
- 我們還可以專門使用 id 屬性來建立 css 表示式。對於 id,css 表示式的格式應為 tagname#id。例如,input#txt [此處 input 是 tagname,txt 是 id 屬性的值]。
- 對於 class,css 表示式的格式應為 tagname.class。例如,input.cls-txt [此處 input 是 tagname,cls-txt 是 class 屬性的值]。
- 如果一個父元素有 n 個子元素,並且我們想要識別第 n 個子元素,那麼 CSS 表示式應該為 nth-of-type(n)。類似地,要識別最後一個子元素,CSS 表示式應為 ul.reading li:last-child。
對於值動態變化的屬性,我們可以使用 ^= 來定位屬性值以特定文字開頭的元素。例如,input[name^='qa'] 這裡,input 是標籤名,name 屬性的值以 qa 開頭。
對於值動態變化的屬性,我們可以使用 $= 來定位屬性值以特定文字結尾的元素。例如,input[class$='txt'] 這裡,input 是標籤名,class 屬性的值以 txt 結尾。
對於值動態變化的屬性,我們可以使用 *= 來定位屬性值包含特定子文字的元素。例如,input[name*='nam'] 這裡,input 是標籤名,name 屬性的值包含子文字 nam。
讓我們獲取如下所示 Name 標籤旁邊的輸入框的 HTML 程式碼:

上面圖片中高亮的編輯框有一個名為 name 的屬性,其值為 name,CSS 表示式應為 input[name='name']。
XPath 定位器
元素的 XPath 定位器值可以用於識別該元素。在 Java 中,方法 findElement(By.xpath("<value of xpath>")) 用於定位具有 XPath 值的元素。使用此方法,可以識別具有匹配 XPath 值的第一個元素。如果不存在具有類似 XPath 值的元素,則會丟擲 NoSuchElementException 異常。
語法
Webdriver driver = new ChromeDriver(); driver.findElement(By.xpath("value of xpath”));
建立 XPath 表示式的規則
要使用 XPath 定位元素,表示式應為 //tagname[@attribute='value']。XPath 可以分為兩種型別:相對 XPath 和絕對 XPath。絕對 XPath 以 / 符號開頭,從根節點開始到要定位的元素。
例如:
/html/body/div[1]/div[2]/div[1]/input
相對 XPath 以 // 符號開頭,並且不從根節點開始。
例如:
//img[@alt='TutorialsPoint']
讓我們深入瞭解高亮連結 - Home 的 HTML 程式碼,從根節點開始。

此元素的絕對 XPath 為:
/html/body/main/div/div/div[2]/p[1]/a
元素 Home 的相對 XPath 為:
//a[@href='https://tutorialspoint.tw/index.htm']
XPath 函式
還有一些可用的函式可以幫助構建相對 xpath 表示式。
text()
它用於定位網頁上具有可見文字的元素。Home 連結的 XPath 表示式為:
//*[text()='Home']
starts-with()
它用於識別屬性值以特定文字開頭的元素。此函式通常用於屬性值在每次頁面載入時都會發生變化的情況。
讓我們研究連結 Moved 的 HTML 程式碼:

XPath 表示式為:
//a[starts-with(@id, 'mov')].
結論
本教程全面介紹了 Selenium WebDriver 定位策略。我們從描述 Id 定位器、Name 定位器、Class Name 定位器、TagName 定位器、Link Text 定位器、Partial Link Text 定位器、CSS 定位器、XPath 定位器開始,並提供了示例說明如何將它們與 Selenium 一起使用。這使您深入瞭解 Selenium WebDriver 定位策略。建議您不斷練習所學知識,並探索與 Selenium 相關的其他知識,以加深理解並拓寬視野。