
- 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 - 斷言/驗證方法
- 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 雜項概念
- Selenium - IE 驅動程式
- Selenium - 自動化框架
- Selenium - 關鍵字驅動框架
- Selenium - 資料驅動框架
- Selenium - 混合驅動框架
- Selenium - SSL 證書錯誤
- Selenium - 替代方案
- Selenium 有用資源
- Selenium - 問答
- Selenium - 快速指南
- Selenium - 有用資源
- Selenium - 自動化實踐
- Selenium - 討論
Selenium WebDriver - 第一個測試指令碼
在開始使用 Selenium 的第一個測試指令碼之前,我們應該確保 Java 和 Maven 已安裝在我們的系統中。
建立第一個測試指令碼的先決條件步驟
步驟 1 - 透過執行命令:java(在命令提示符中)確認 Java 安裝。
步驟 2 - 透過執行命令確認已安裝的 Java 版本
java –version
步驟 3 - 透過執行命令確認已安裝的Maven版本 -
mvn –version
步驟 4 - 從連結下載 Eclipse 技術下載並安裝任何程式碼編輯器,例如 Eclipse。
要獲取有關 Eclipse 及其安裝過程的更多資訊,請訪問連結Eclipse 教程。
建立第一個測試指令碼的步驟
步驟 1 - 啟動驅動程式會話並在瀏覽器上執行某些操作,例如使用以下程式碼在 Chrome 瀏覽器上啟動網頁 -
Webdriver driver = new ChromeDriver(); driver.get("https://tutorialspoint.tw/selenium/practice/text-box.php");
步驟 2 - 使用 getCurrentUrl() 方法檢索有關在 Chrome 瀏覽器上啟動的當前 URL 的一些資訊。
driver.getCurrentUrl();
步驟 3 - 新增一些同步技術,以便 Selenium 工具和瀏覽器操作之間的互動保持同步。Selenium 中有幾種同步方法可用,例如隱式、顯式和流暢等待。
雖然隱式等待是一種全域性等待(適用於測試用例中的所有步驟),但顯式和流暢等待應用於瀏覽器上的特定元素。對於我們這裡的第一個測試指令碼,我們只應用隱式等待。
新增 15 秒隱式等待的語法 -
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
要獲取有關 Selenium 中不同等待的更多資訊,請訪問連結Selenium Webdriver 等待支援。
步驟 4 - 在同一個 webdriver 會話中對 webelement 執行任何任務,為此,使用任何Selenium 定位器(如 id、name、class、tagname、link text、partial link text、xpath 和 css 選擇器)以及 findElement() 方法唯一地識別該元素。如果有多個元素具有相同的值的定位器,則應僅識別第一個匹配的元素。
使用 xpath 定位器識別元素的語法 -
WebElement e = driver.findElement(By.xpath("<value of xpath>"));
步驟 5 - 使用 sendKeys() 方法對上一步中識別的元素執行任務。輸入文字 - Selenium,要輸入的文字作為引數傳遞給該方法。
語法
e.sendKeys("Selenium");
步驟 6 - 使用 getAttribute() 方法請求輸入框中輸入的值的資訊,並將值作為引數傳遞給該方法。
語法
String text = driver.findElement(By.xpath("<value of xpath>")).getAttribute("value");
步驟 7 - 使用clear()方法清除輸入的文字。
語法
e.clear();
步驟 8 - 再次使用 getAttribute() 方法請求輸入框中值的資訊(以證明在使用 clear() 方法後輸入框中沒有值),並將值作為引數傳遞給該方法。
語法
String text = driver.findElement(By.xpath("<value of xpath>")).getAttribute("value");
步驟 9 - 使用 quit() 方法終止驅動程式會話,這也會關閉已開啟的瀏覽器視窗。
語法
driver.quit();
示例
右鍵單擊下面的網頁,然後在 Chrome 瀏覽器中單擊“檢查”按鈕。要識別頁面上的第一個文字框,請單擊突出顯示的可見 HTML 程式碼頂部的左上箭頭。

一旦我們單擊並指向輸入框(在下圖中突出顯示),其 HTML 程式碼就會出現,反映輸入標籤名稱和型別屬性的值為文字。

讓我們以以上頁面為例,我們首先使用sendKeys()方法在文字框中輸入文字。稍後,我們將使用clear()方法清除文字。
程式碼實現
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 FrstJavaTestScript { 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); // Opening the webpage where we will identify edit box to enter driver.get("https://tutorialspoint.tw/selenium/practice/text-box.php"); // getting current URL System.out.println("Getting the Current URL: " + driver.getCurrentUrl()); // Identify the input box with xpath locator WebElement e = driver.findElement(By.xpath("//*[@id='fullname']")); // enter text in input box e.sendKeys("First Test Script"); // Get the value entered String text = e.getAttribute("value"); System.out.println("Entered text is: " + text); // clear the text entered e.clear(); // Get no text after clearing text String text1 = e.getAttribute("value"); System.out.println("Get text after clearing: " + text1); // Closing browser driver.quit(); } }
輸出
Getting the Current URL: https://tutorialspoint.tw/selenium/practice/text-box.php Entered text is: First Test Script Get text after clearing: Process finished with exit code 0
在以上示例中,我們首先獲得了當前 URL,控制檯中的訊息為:獲取當前 URL:Selenium 自動化實踐表單。然後,我們在輸入框中輸入了文字第一個測試指令碼,我們使用訊息 - 輸入的文字為:第一個測試指令碼在控制檯中檢索了輸入的值。接下來,我們清除了輸入的值,因此在同一個文字框中沒有獲得任何值。
最後,收到了訊息程序已完成,退出程式碼為 0,表示程式碼已成功執行。
結論
這結束了我們對 Selenium Webdriver 第一個測試指令碼教程的全面介紹。我們首先描述了建立第一個測試指令碼的先決條件步驟、建立第一個測試指令碼的步驟以及一個示例來說明如何在 Selenium Webdriver 中建立第一個測試指令碼。這使您深入瞭解 Selenium Webdriver - 第一個測試指令碼。明智的做法是不斷練習您學到的知識,並探索與 Selenium 相關的其他知識,以加深您的理解並拓寬您的視野。