- 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 - 動態網頁表格
- 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 可用於各種使用者互動,例如點選連結或按鈕,在輸入框中輸入或清除文字,以及在下拉列表中選擇或取消選擇選項。
使用者互動的基本 Selenium Webdriver 命令
Selenium Webdriver 中有多個命令可以幫助對元素執行操作。它們列在下面 -
click()
此方法用於在元素的中間執行點選事件。如果元素的中間隱藏,則會丟擲異常。
右鍵點選網頁,然後在 Chrome 瀏覽器中點選“檢查”按鈕。然後,將開啟該頁面的相關 HTML 程式碼。要檢查下面頁面中的某個連結,例如無內容,請點選 HTML 程式碼頂部突出顯示的向上箭頭。
一旦我們點選並指向了“無內容”超連結的箭頭,它的 HTML 程式碼就可見了。可以使用連結文字定位器檢測連結。使用此方法,將定位具有連結文字匹配值的第一個連結。
示例
讓我們舉個例子,我們首先點選無內容連結,然後我們將獲得文字連結已使用狀態 204 和狀態文字進行響應。
程式碼實現
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 Lnks {
public static void main(String[] args) throws InterruptedException {
// Initiate the Webdriver
WebDriver driver = new EdgeDriver();
// adding implicit wait of 15 secs
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
// Opening the webpage where we will identify link
driver.get("https://tutorialspoint.tw/selenium/practice/links.php");
// Identify link with link text
driver.findElement(By.linkText("No Content")).click();
// identify text
WebElement l = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/div[2]"));
// Get text after clicking the link
System.out.println("Text is: " + l.getText());
// Closing browser
driver.quit();
}
}
輸出
Text is: Link has responded with status 204 and status text No Content Process finished with exit code 0
在上面的示例中,點選無內容連結後獲得的文字為文字為:連結已使用狀態 204 和狀態文字無內容進行響應。
最後,收到訊息程序已完成,退出程式碼為 0,表示程式碼已成功執行。
sendKeys()
此方法用於在編輯框或任何可編輯欄位中輸入一些文字。每個輸入框都具有名為 input 的標籤名。此外,輸入框應具有 type 屬性,其值為 text。如果元素不可編輯,並且在該元素上應用了 sendKeys() 方法,則應丟擲異常。
clear()
此方法用於清除編輯框或任何可編輯欄位中的文字。如果元素不可編輯,並且在該元素上應用了 clear() 方法,則應丟擲異常。
示例
讓我們以上面頁面的一個示例為例,我們首先使用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 InptBox {
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 and enter
driver.get("https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php");
// Identify the input box with xpath locator
WebElement e = driver.findElement(By.xpath("//*[@id='name']"));
// enter text in input box
e.sendKeys("Selenium");
// 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();
}
}
輸出
Entered text is: Selenium Get text after clearing: Process finished with exit code 0
在上面的示例中,我們首先在輸入框中輸入了文字Selenium,並在控制檯中檢索了輸入的值,訊息為 - 輸入的文字為:Selenium。然後清除輸入的值,並且輸入框中沒有值。因此,我們在控制檯中收到以下訊息:清除後獲取文字。
Select 類
Select 類中有多個方法可以幫助我們與下拉列表進行互動。每個下拉列表都由 select 標籤名標識,其選項由 option 標籤名標識。網頁上可能有兩種型別下拉列表 - 單選(允許選擇一個選項)和多選(允許選擇多個選項)。多選下拉列表還應具有一個屬性 multiple。
在上圖中,選項選擇一個標題具有屬性 selected,這意味著預設情況下已選中此選項。Select 類中有多個方法允許處理Selenium Webdriver 中的下拉列表。
在下面的頁面中,我們將訪問“多選下拉列表”旁邊的多選下拉列表,選擇值電子產品和電腦和運動和戶外,並執行一些驗證。
示例
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.support.ui.Select;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class SelectMultipleDropdowns {
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 get dropdown
driver.get("https://tutorialspoint.tw/selenium/practice/select-menu.php");
// identify multiple dropdown
WebElement dropdown = driver.findElement
(By.xpath("//*[@id='demo-multiple-select']"));
// object of Select class
Select select = new Select(dropdown);
// gets options of dropdown in list
List<WebElement> options = select.getOptions();
for (WebElement opt : options){
System.out.println("Options are: " + opt.getText());
}
// return true if multi-select dropdown
Boolean b = select.isMultiple();
System.out.println("Boolean value for multiple dropdown: "+ b);
// select item by value
select.selectByValue("3");
// select item by index
select.selectByIndex(7);
// get all selected options of dropdown in list
List<WebElement> selectedOptions = select.getAllSelectedOptions();
for (WebElement opt : selectedOptions){
System.out.println("Selected Options are: " + opt.getText());
}
// get first selected option in dropdown
WebElement f = select.getFirstSelectedOption();
System.out.println("First selected option is: "+ f.getText());
// deselect option by index
select.deselectByIndex(7);
// deselect all selected items
select.deselectAll();
// get all selected options of dropdown after deselected
List<WebElement> delectedOptions = select.getAllSelectedOptions();
System.out.println("No. options selected: " + delectedOptions.size());
// Closing browser
driver.quit();
}
}
輸出
Options are: Books Options are: Movies, Music & Games Options are: Electronics & Computers Options are: Home, Garden & Tools Options are: Health & Beauty Options are: Toys, Kids & Baby Options are: Clothing & Jewelry Options are: Sports & Outdoors Boolean value for multiple dropdown: true Selected Options are: Electronics & Computers Selected Options are: Sports & Outdoors First selected option is: Electronics & Computers No. options selected: 0 Process finished with exit code 0
在上面的示例中,我們使用控制檯中的訊息獲取了下拉列表的所有選項 - 選項為:書籍,選項為:電影,音樂和遊戲,選項為:家居,花園和工具,選項為:健康和美容,選項為:玩具,兒童和嬰兒,選項為:服裝和珠寶,選項為:運動和戶外。我們還使用控制檯中的訊息驗證了下拉列表是否具有多個選擇選項 - 用於檢查的布林值為:true。
我們使用控制檯中的訊息檢索了下拉列表中選定的選項 - 選定的選項為:電子產品和電腦,選定的選項為:運動和戶外。我們還使用控制檯中的訊息獲取了第一個選定的選項 - 第一個選定的選項為:電子產品和電腦。最後,我們取消選擇了下拉列表中所有選定的選項,因此在控制檯中獲得了訊息 - 選定的選項數:0。
滾動操作
Selenium webdriver 可以使用 JavaScriptExecutor(一個介面)在網頁上執行滾動操作。Selenium Webdriver 可以使用 JavaScriptExecutor 執行 JavaScript 命令。
閱讀更多:Selenium Webdriver 滾動操作.
結論
本教程全面介紹了 Selenium WebDriver 使用者互動。我們從描述使用者互動的基本 Selenium Webdriver 命令開始,並提供示例來說明如何在 Selenium Webdriver 中使用這些命令。這將使您深入瞭解 Selenium WebDriver 使用者互動。建議您不斷練習所學內容,並探索與 Selenium 相關的其他內容,以加深理解並拓寬視野。
