- 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 - WebElement 命令
Selenium Webdriver 可用於提取有關網頁上特定元素的多種資訊。例如,我們可以驗證元素在網頁上是否存在/不存在,已啟用/已停用或已選中/未選中。
Selenium Webdriver 中的基本 WebElement 命令
- 要驗證元素是否存在於網頁上,我們可以藉助 **isDisplayed()** 方法。如果元素存在,isDisplayed() 將返回 true,否則返回 false。
- 要驗證元素是否在網頁上被選中,我們可以藉助 **isSelected()** 方法。如果元素被選中,isSelected() 將返回 true,否則返回 false。
- 要驗證元素是否在網頁上已啟用,我們可以藉助 **isEnabled()** 方法。如果元素已啟用,isEnabled() 將返回 true,否則返回 false。
- 要獲取元素的標籤名稱,我們可以藉助 **getTagName()** 方法。
- 要獲取元素的 x 和 y 座標,我們可以藉助 **getRect()** 方法。
- 要獲取元素的背景顏色或顏色,我們可以藉助 **getCssValue()** 方法。
- 要獲取與 DOM 關聯的元素的執行時值,我們可以藉助 **getAttribute()** 方法並將值作為引數傳遞給該方法。
- 要獲取元素的文字,我們可以藉助 **getText()** 方法。
識別 HTML 中的單選按鈕
右鍵單擊網頁,然後單擊 Chrome 瀏覽器中的“檢查”按鈕。要檢查頁面上的單選按鈕,請單擊位於可見 HTML 程式碼頂部的左上方箭頭,如下所示。
單擊並將箭頭指向單選按鈕(如下圖所示)後,可以看到其 HTML 程式碼,其中反映了輸入標籤名稱和 type 屬性的值為 radio。
示例 1
讓我們以以上頁面為例,我們將使用 click() 方法單擊其中一個單選按鈕。然後,我們將使用上述方法驗證單選按鈕是否存在、已啟用和已選中。
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 ValidatingRadioButtons {
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 click radio button
driver.get("https://tutorialspoint.tw/selenium/practice/radio-button.php");
// identify radio button then click
WebElement radiobtn = driver.findElement
(By.xpath("/html/body/main/div/div/div[2]/form/div[1]/input"));
radiobtn.click();
// verify if radio button is selected
boolean result = radiobtn.isSelected();
System.out.println("Checking if a radio button is selected: " + result);
// verify if radio button is displayed
boolean result1 = radiobtn.isDisplayed();
System.out.println("Checking if a radio button is displayed: " + result1);
// verify if radio button is enabled
boolean result2 = radiobtn.isEnabled();
System.out.println("Checking if a radio button is enabled: " + result2);
// identify another radio button
WebElement radiobtn1 = driver.
findElement(By.xpath("/html/body/main/div/div/div[2]/form/div[5]/input"));
// verify if radio button is disabled
boolean result3 = radiobtn1.isEnabled();
System.out.println("Checking if the other radio button is disabled: " + result3);
// verify if radio button is unselected
boolean result4 = radiobtn1.isEnabled();
System.out.println("Checking if the other radio button is unselected: " + result4);
// Closing browser
driver.quit();
}
}
輸出
Checking if a radio button is selected: true Checking if a radio button is displayed: true Checking if a radio button is enabled: true Checking if the other radio button is disabled: false Checking if the other radio button is unselected: false Process finished with exit code 0
在上面的示例中,我們首先單擊了一個單選按鈕,然後在控制檯中使用訊息驗證該單選按鈕是否存在、是否已選中和是否已啟用 - **正在檢查單選按鈕是否已選中:true,正在檢查單選按鈕是否已顯示:true,正在檢查單選按鈕是否已啟用:true**。
然後,我們驗證了另一個單選按鈕是否已停用和未選中,控制檯中的訊息為 - **正在檢查另一個單選按鈕是否已停用:false,以及正在檢查另一個單選按鈕是否未選中:false**。
最後,收到訊息 **程序已完成,退出程式碼為 0**,表示程式碼已成功執行。
示例 2
我們可以使用 **getTagName()** 方法獲取單選按鈕的標籤名稱。
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 RadioButtonTagNames {
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 click radio button
driver.get("https://tutorialspoint.tw/selenium/practice/radio-button.php");
// identify radio button then click
WebElement radiobtn = driver.findElement
(By.xpath("/html/body/main/div/div/div[2]/form/div[1]/input"));
String text = radiobtn.getTagName();
System.out.println("Get the radio button tagname: " + text);
// Closing browser
driver.quit();
}
}
輸出
Get the radio button tagname: input Process finished with exit code 0
在上面的示例中,我們在控制檯中獲得了單選按鈕的標籤名稱,訊息為 - **獲取單選按鈕標籤名稱:input**。
示例 3
讓我們在下面的圖片中再舉一個例子,我們將使用 **getRect()** 方法獲取頁面上文字 **Selenium - 自動化實踐表單** 的高度、寬度、x 和 y 座標。
程式碼實現
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.Rectangle;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class ElementPositions {
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
driver.get("https://tutorialspoint.tw/selenium/practice/radio-button.php");
// Identify the element with xpath locator
WebElement e = driver.findElement(By.xpath("/html/body/div/header/div[2]/h1"));
Rectangle res = e.getRect();
// Getting height, width, x, and y coordinates of element
System.out.println("Getting height: " + res.getHeight());
System.out.println("Getting width: " + res.getWidth());
System.out.println("Getting x-cordinates: " + res.getX());
System.out.println("Getting y-cordinates: " + res.getY());
// Closing browser
driver.quit();
}
}
輸出
Getting height: 29 Getting width: 395 Getting x-cordinates: 453 Getting y-cordinates: 18 Process finished with exit code 0
在上面的示例中,我們在控制檯中獲得了文字的高度、寬度、x 和 y 座標,訊息為 - **獲取高度:29,獲取寬度:395,獲取 x 座標:453,以及獲取 y 座標:18**。
示例 4
讓我們在下面的圖片中再舉一個例子,我們將獲取突出顯示的元素 **登入**(在“樣式”部分中顯示)的背景顏色。我們將藉助 **getCssValue()** 方法並將 background-color 作為引數傳遞給該方法。
程式碼實現
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 ElementsCSSProperty {
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
driver.get("https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php");
// Identify the element with xpath locator
WebElement e = driver.findElement(By.xpath("//*[@id='practiceForm']/div[11]/input"));
// Getting background color of the element
System.out.println ("Getting background color of element: " + e.getCssValue("background-color"));
// Closing browser
driver.quit();
}
}
輸出
Getting background color of element: rgba(13, 110, 253, 1) Process finished with exit code 0
在上面的示例中,我們以 rgb 格式在控制檯中獲得了頁面上元素的背景,訊息為 - **獲取元素的背景顏色:rgba(13, 110, 253, 1)**。
示例 5
讓我們以以下頁面為例,我們將首先使用 sendKeys() 方法在輸入框中輸入文字 **Selenium**。然後,我們將使用 **getAttribute()** 方法獲取與 DOM 關聯的元素的執行時值,並將值作為引數傳遞。
程式碼實現
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 InputBoxs {
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
driver.get("https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php");
// Identify the element with xpath locator
WebElement em = driver.findElement
(By.xpath("//*[@id='name']"));
// enter text in input box
em.sendKeys("Selenium");
// Get the value entered
String t = em.getAttribute("value");
System.out.println("Text entered: " + t);
// clear the text entered
em.clear();
// Get no text after clearing text
String t1 = e.getAttribute("value");
System.out.println("Get text after clearing: " + t1);
// Closing browser
driver.quit();
}
}
輸出
Entered text is: Selenium Get text after clearing: Process finished with exit code 0
在上例中,我們首先在輸入框中輸入文字Selenium,並在控制檯中檢索輸入的值,訊息為:輸入文字:Selenium。然後清除輸入的值,輸入框中沒有值。因此,我們還在控制檯中收到了訊息:清除後獲取文字:。
示例 6
讓我們來看一下下面頁面的例子,我們將使用getText()方法獲取突出顯示元素的文字 - 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 ElementsText {
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
driver.get("https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php");
// Identify the element with xpath locator
WebElement e = driver.findElement
(By.xpath("/html/body/div/header/div[2]/h1"));
// Getting text of the element
System.out.println
("Getting element text: " + e.getText());
// Closing browser
driver.quit();
}
}
輸出
Getting element text: Selenium - Automation Practice Form Process finished with exit code 0
在上例中,我們在控制檯中獲得了元素的文字,訊息為 - 獲取元素文字:Selenium - 自動化實踐表單。
結論
本教程對Selenium WebDriver WebElement命令進行了全面的講解。我們從描述HTML中如何識別單選按鈕開始,講解了Selenium Webdriver中的基本Web元素命令,並透過示例說明了如何在Selenium Webdriver中使用它們。這使您能夠深入瞭解WebDriver WebElement命令。最好繼續練習您學到的知識,並探索與Selenium相關的其他知識,以加深您的理解並拓寬您的視野。
