
- 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 WebDriver - 日期時間選擇器
Selenium Webdriver 可用於從日曆中選擇日期和時間。Web UI 上可以以多種方式設計日曆中的日期時間選擇器。根據 UI,需要設計測試用例。
什麼是日期時間選擇器?
日期時間選擇器基本上是在被測應用程式中建立日曆的使用者介面。日期時間選擇器的功能是從一個範圍內選擇特定的日期和/或時間。
如何檢查日期時間選擇器?
要識別下圖中顯示的日曆,首先我們會點選“選擇日期”欄位下的第一個輸入框,然後右鍵點選網頁,最後點選 Chrome 瀏覽器中的“檢查”按鈕。要調查頁面上的日曆,請點選 HTML 程式碼頂部可用的左上箭頭,如下所示。

過去日期選擇
讓我們以下面頁面中的日期時間選擇器為例,它具有選擇年份、月份、日期、小時、分鐘等選項。它還包含選擇過去或未來日期和時間的選項。讓我們選擇一個過去的日期和時間 - 上午 2023 年 6 月 4 日 09:05,然後驗證它們。

示例
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.concurrent.TimeUnit; public class DatTimePickers { 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); // URL launch for accessing calendar driver.get("https://tutorialspoint.tw/selenium/practice/date-picker.php"); // identify element to get calendar WebElement f = driver .findElement(By.xpath("//*[@id='datetimepicker2']")); f.click(); // selecting month June WebElement month = driver .findElement(By.xpath("/html/body/div[3]/div[1]/div/div/select")); Select select = new Select(month); select.selectByVisibleText("June"); // getting selected month String selectedMonth = select.getFirstSelectedOption().getText(); // selecting year 2023 WebElement year = driver.findElement(By.xpath("/html/body/div[3]/div[1]/div/div/div/input")); // removing existing year then entering year.clear(); year.sendKeys("2023"); // selecting day 4 WebElement day = driver.findElement(By.xpath("//span[contains(@aria-label,'"+selectedMonth+" 4')]")); day.click(); // selecting AM time WebElement time = driver.findElement(By.xpath("/html/body/div[3]/div[3]/span[2]")); if (time.getText().equalsIgnoreCase("PM")){ time.click(); } // selecting hour WebElement hour = driver .findElement(By.xpath("/html/body/div[3]/div[3]/div[1]/input")); // removing existing hour then entering hour.clear(); hour.sendKeys("9"); // selecting minutes WebElement minutes = driver.findElement(By.xpath("/html/body/div[3]/div[3]/div[2]/input")); // removing existing minutes then entering minutes.clear(); minutes.sendKeys("5"); // reflecting both date and time f.click(); // get date and time selected String v = f.getAttribute("value"); System.out.println("Date and time selected by Date Time Picker: " + v); // check date and time selected if (v.equalsIgnoreCase("2023-06-04 09:05")){ System.out.print("Date and Time selected successfully"); } else { System.out.print("Date and Time selected unsuccessfully"); } // close browser driver.quit(); } }
輸出
Date and time selected by Date Time Picker: 2023-06-04 09:05 Date and Time selected successfully Process finished with exit code 0
在上面的示例中,我們使用日期時間選擇器從日曆中選擇了過去的日期和時間,然後透過訊息驗證是否選擇了正確的日期 - **日期時間選擇器選擇的日期和時間:2023-06-04 09:05** 和 **日期和時間選擇成功**。
最後,收到訊息 **程序已完成,退出程式碼為 0**,表示程式碼成功執行。
未來日期選擇
讓我們再舉一個下面頁面中日期時間選擇器的例子,我們將選擇一個未來的日期和時間 - 晚上 2025 年 6 月 4 日 21:05,然後驗證它們。

示例
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.concurrent.TimeUnit; public class DatTimePickersForward { 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); // URL launch for accessing calendar driver.get("https://tutorialspoint.tw/selenium/practice/date-picker.php"); // identify element to get calendar WebElement f = driver.findElement(By.xpath("//*[@id='datetimepicker2']")); f.click(); // selecting month June WebElement month = driver.findElement(By.xpath("/html/body/div[3]/div[1]/div/div/select")); Select select = new Select(month); select.selectByVisibleText("June"); // getting selected month String selectedMonth = select.getFirstSelectedOption().getText(); // selecting year 2023 WebElement year = driver.findElement(By.xpath("/html/body/div[3]/div[1]/div/div/div/input")); // removing existing year then entering year.clear(); year.sendKeys("2025"); // selecting day 4 WebElement day = driver.findElement(By.xpath ("//span[contains(@aria-label,'"+selectedMonth+" 4')]")); day.click(); // selecting PM time WebElement time = driver.findElement(By.xpath("/html/body/div[3]/div[3]/span[2]")); if (time.getText().equalsIgnoreCase("AM")){ time.click(); } // selecting hour WebElement hour = driver.findElement(By.xpath("/html/body/div[3]/div[3]/div[1]/input")); // removing existing hour then entering hour.clear(); hour.sendKeys("9"); // selecting minutes WebElement minutes = driver.findElement(By.xpath("/html/body/div[3]/div[3]/div[2]/input")); // removing existing minutes then entering minutes.clear(); minutes.sendKeys("5"); // reflecting both date and time f.click(); // get date and time selected String v = f.getAttribute("value"); System.out.println("Date and time selected by Date Time Picker: " + v); // check date and time selected if (v.equalsIgnoreCase("2025-06-04 21:05")){ System.out.print("Date and Time selected successfully"); } else { System.out.print("Date and Time selected unsuccessfully"); } // close browser driver.quit(); } }
輸出
Date and time selected by Date Time Picker: 2025-06-04 21:05 Date and Time selected successfully Process finished with exit code 0
在上面的示例中,我們使用日期時間選擇器從日曆中選擇了未來的日期和時間,然後透過訊息驗證是否選擇了正確的日期 - **日期時間選擇器選擇的日期和時間:2025-06-04 21:05** 和 **日期和時間選擇成功**。
最後,收到訊息 **程序已完成,退出程式碼為 0**,表示程式碼成功執行。
當前日期選擇
讓我們再舉一個下面頁面中日期時間選擇器的例子,我們將選擇一個當前日期的日期和時間,然後驗證它們。請注意,在此日期時間選擇器中,預設情況下會選擇當前日期,我們只需要選擇當前時間即可。

示例
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.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.concurrent.TimeUnit; public class DateTimePickersCurrent { 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); // URL launch for accessing calendar driver.get("https://tutorialspoint.tw/selenium/practice/date-picker.php"); // get current time LocalDateTime l = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); String dateTime = l.format(formatter); String[] dtTime = dateTime.split(" "); String time = dtTime[1]; String[] t = time.split(":"); String hour = t[0]; String minute = t[1]; // identify element to get calendar WebElement f = driver.findElement(By.xpath("//*[@id='datetimepicker2']")); f.click(); // selecting hour based on current WebElement hours = driver.findElement(By.xpath("/html/body/div[3]/div[3]/div[1]/input")); // removing existing hour then entering hours.clear(); hours.sendKeys(hour); // selecting minutes based on current minutes WebElement minutes = driver.findElement(By.xpath("/html/body/div[3]/div[3]/div[2]/input")); // removing existing minutes then entering minutes.clear(); minutes.sendKeys(minute); // reflecting both date and time f.click(); // get date and time selected String v = f.getAttribute("value"); System.out.println("Date and time selected by Date Time Picker: " + v); // check date and time selected if (v.equalsIgnoreCase(dateTime)){ System.out.print("Date and Time selected successfully"); } else { System.out.print("Date and Time selected unsuccessfully"); } // close browser driver.quit(); } }
輸出
Date and time selected by Date Time Picker: 2024-03-13 13:29 Date and Time selected successfully Process finished with exit code 0
在上面的示例中,我們使用日期時間選擇器從日曆中選擇了當前日期和時間,然後透過訊息驗證是否選擇了正確的日期 - **日期時間選擇器選擇的日期和時間:2024-03-13 13:29** 和 **日期和時間選擇成功**。
最後,收到訊息 **程序已完成,退出程式碼為 0**,表示程式碼成功執行。
結論
這總結了我們關於 Selenium Webdriver 日期時間選擇器教程的全面內容。我們首先描述了什麼是日期時間選擇器,如何檢查它以及一些示例來逐步演示如何使用 Selenium 選擇過去、當前和將來的日期和時間。這使您深入瞭解 Selenium 中的日期時間選擇器。明智的做法是不斷練習您學到的知識,並探索與 Selenium 相關的其他知識,以加深您的理解並擴充套件您的視野。