
- 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 其他概念
- Selenium - IE 驅動程式
- Selenium - 自動化框架
- Selenium - 關鍵字驅動框架
- Selenium - 資料驅動框架
- Selenium - 混合驅動框架
- Selenium - SSL 證書錯誤
- Selenium - 替代方案
- Selenium 有用資源
- Selenium - 問答
- Selenium - 快速指南
- Selenium - 有用資源
- Selenium - 自動化實踐
- Selenium - 討論
Selenium WebDriver - 輸入框
Selenium Webdriver 可以用來處理輸入框(也稱為文字框)。在 HTML 術語中,每個輸入框都由名為 input 的標籤名標識。
HTML 中輸入框的識別
開啟任何瀏覽器,例如 Chrome,右鍵單擊網頁,然後單擊“檢查”按鈕。然後,將顯示該頁面的完整 HTML 程式碼。要檢查頁面上的輸入框,請單擊位於 HTML 程式碼頂部的左側向上箭頭,如下面的影像中突出顯示的那樣。

一旦我們單擊並將箭頭指向輸入框(在下面的影像中突出顯示),其 HTML 程式碼就會可見,反映輸入標籤名(括在 <> 中)。

示例 1- 使用 sendKeys 輸入文字
讓我們以上面頁面的一個示例為例,我們首先使用 sendKeys() 方法在 輸入框 中輸入一些文字。然後,我們使用 clear() 方法擦除輸入的文字。
package org.example; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.edge.EdgeDriver; import java.util.concurrent.TimeUnit; public class EnterText{ public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new EdgeDriver(); // adding implicit wait of 10 secs driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Opening the webpage driver.get("https://tutorialspoint.tw/selenium/practice/text-box.php"); // Identify the input box WebElement elm = driver.findElement(By.xpath("//*[@id='fullname']")); // enter text elm.sendKeys("Java"); // Get the value String txt = driver.findElement(By.xpath("//*[@id='fullname']")).getAttribute("value"); System.out.println("Entered text: " + text); // clear the text entered elm.clear(); // Get no text String txt1 = driver. findElement(By.xpath("//*[@id='fullname']")).getAttribute("value"); System.out.println("Get text after clearing: " + txt1); // Closing browser driver.quit(); } }
輸出
Entered text: Java Get text after clearing: Process finished with exit code 0
在上面的示例中,我們首先在編輯框中輸入了文字 Java,並在控制檯中獲取了輸入的值,訊息為 - 輸入的文字:Java。然後清除輸入的值,並在刪除文字後在編輯框中沒有獲取任何值,並在控制檯中收到訊息:清除後獲取文字:。
最後,收到訊息 程序已完成,退出程式碼為 0,表示程式碼成功執行。
示例 2 - 使用 Actions 類輸入文字
我們還可以將 sendKeys() 方法與 Selenium 中的 Actions 類 一起使用。我們將採用上面討論的相同示例並檢視實現。
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.interactions.Actions; import java.util.concurrent.TimeUnit; public class HandlingInputBoxWithActions { 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"); // Identify the input box with xpath locator WebElement e = driver.findElement(By.xpath("//*[@id='fullname']")); // enter text in input box using Actions class Actions action = new Actions(driver); action.sendKeys(e, "SeleniumS").perform(); // Get the value entered String text = driver. findElement(By.xpath("//*[@id='fullname']")).getAttribute("value"); System.out.println("Entered text with Actions class is: " + text); // clear the text entered e.clear(); // Get no text after clearing text String text1 = driver. findElement(By.xpath("//*[@id='fullname']")).getAttribute("value"); System.out.println("Get text after clearing: " + text1); // Closing browser driver.quit(); } }
輸出
Entered text with Actions class is: SeleniumS Get text after clearing: Process finished with exit code 0
在上面的示例中,我們首先在輸入框中輸入了文字 SeleniumS,並在控制檯中獲取了輸入的值,訊息為 - 使用 Actions 類輸入的文字為:SeleniumS。然後清除輸入的值,並在清除文字後在輸入框中沒有獲取任何值。因此,我們還在控制檯中收到了訊息:清除後獲取文字:。
示例 3 - 使用 JavaScriptExecutor 輸入文字
我們還可以使用 JavaScriptExecutor 在 Selenium 中的輸入框中輸入文字。我們將採用上面討論的相同示例。
package org.example; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class HandlingInputBoxWithJS { 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"); // Identify the input box with xpath locator WebElement e = driver.findElement(By.xpath("//*[@id='fullname']")); // enter text in input box using JavascriptExecutor JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver; javascriptExecutor.executeScript("arguments[0].setAttribute('value', 'Selenium Java')", e); // Get the value entered String text = driver.findElement(By.xpath("//*[@id='fullname']")).getAttribute("value"); System.out.println("Entered text with JavaScript Executor is: " + text); // clear the text entered e.clear(); // Get no text after clearing text String text1 = driver. findElement(By.xpath("//*[@id='fullname']")).getAttribute("value"); System.out.println("Get text after clearing: " + text1); // Closing browser driver.quit(); } }
輸出
Entered text with JavaScript Executor is: Selenium Java Get text after clearing: Process finished with exit code 0
在上面的示例中,我們首先在輸入框中輸入了文字 Selenium Java,並在控制檯中獲取了輸入的值,訊息為 - 使用 JavaScript Executor 輸入的文字為:Selenium Java。然後清除輸入的值,並在輸入框中沒有獲取任何值,然後在控制檯中收到訊息:清除後獲取文字:。
結論
這總結了我們對 Selenium Webdriver 輸入框教程的全面介紹。我們從描述 HTML 中輸入框的識別開始,並透過示例說明如何使用 sendKeys 方法、Actions 類和 Selenium Webdriver 中的 JavaScript 在輸入框中輸入文字。這使您能夠深入瞭解 Selenium Webdriver 輸入框。明智的做法是不斷練習您所學到的知識,並探索與 Selenium 相關的其他內容,以加深您的理解並拓寬您的視野。