Đối với lựa chọn DateTime, HTML5 có một điều khiển mới được hiển thị bên dưới.
Trang trên có thể được truy cập tại đây
Nếu chúng ta thấy DOM của điều khiển DateTime Picker, sẽ chỉ có một hộp nhập cho cả ngày và giờ.
Vì vậy, để xử lý loại điều khiển này, đầu tiên chúng ta sẽ điền ngày tháng mà không phân tách bằng dấu phân cách, tức là nếu ngày là 25/09/2013, thì chúng ta sẽ chuyển 09252013 vào hộp nhập liệu. Sau khi hoàn tất, chúng tôi sẽ chuyển trọng tâm từ ngày này sang thời gian khác bằng cách nhấn 'tab' và điền thời gian.
Nếu chúng ta cần điền vào 02:45 PM, chúng ta sẽ chuyển nó là '0245PM' vào cùng một hộp nhập liệu.
Mã cho người chọn ngày trông như thế này -
nhập java.util.List;nhập java.util.concurrent.TimeUnit;nhập org.openqa.selenium.By;nhập org.openqa.selenium.Keys;nhập org.openqa.selenium.WebDriver;nhập org.openqa.selenium.WebElement;nhập org.openqa.selenium.chrome.ChromeDriver;nhập org.testng.annotations.Test;lớp công khai DateTimePicker {@Kiểm trapublic void dateTimePicker () {System.setProperty ("webdriver.chrome.driver", "chromedriver.exe");Trình điều khiển WebDriver = mới ChromeDriver ();driver.manage (). timeouts (). implicitWait (10, TimeUnit.SECONDS);driver.get ("http://demo.guru99.com/test/");// Tìm điều khiển bộ chọn ngày giờWebElement dateBox = driver.findElement (By.xpath ("// form // input [@ name = 'bdaytime']"));// Điền ngày tháng là mm / dd / yyyy là 25/09/2013dateBox.sendKeys ("09252013");// Nhấn tab để chuyển tiêu điểm sang trường thời giandateBox.sendKeys (Keys.TAB);// Lấp đầy thời gian là 02:45 PMdateBox.sendKeys ("0245PM");}}
Đầu ra sẽ như-
Hãy xem một ví dụ Lịch khác. Chúng tôi sẽ sử dụng điều khiển Telerik DateTimePicker. Có thể truy cập tại đây
Ở đây, nếu chúng ta cần thay đổi tháng, chúng ta phải nhấp vào giữa tiêu đề lịch.
Tương tự, nếu chúng ta cần thay đổi năm thì chúng ta có thể thực hiện bằng cách nhấp vào liên kết tiếp theo hoặc trước đó trên bộ chọn ngày.
Và cuối cùng để thay đổi thời gian, chúng ta có thể chọn thời gian chính xác từ menu thả xuống (Lưu ý: Ở đây thời gian được chọn trong khoảng cách 30 phút. Tức là 12:00, 12:30, 1:00, 1:30, v.v.).
Một ví dụ hoàn chỉnh trông giống như-
nhập java.util.Calendar;nhập java.util.List;nhập java.util.concurrent.TimeUnit;nhập org.openqa.selenium.By;nhập org.openqa.selenium.WebDriver;nhập org.openqa.selenium.WebElement;nhập org.openqa.selenium.firefox.FirefoxDriver;nhập org.testng.annotations.Test;lớp công khai DatePicker {@Kiểm trapublic void testDAtePicker () ném Exception {// DAte và thời gian được đặt trong hộp văn bảnString dateTime = "12/07/2014 2:00 PM";Trình điều khiển WebDriver = mới FirefoxDriver ();driver.manage (). window (). Maxim ();driver.get ("https://demos.telerik.com/kendo-ui/datetimepicker/index");driver.manage (). timeouts (). implicitWait (10, TimeUnit.SECONDS);// nút để mở lịchWebElement selectDate = driver.findElement (By.xpath ("// span [@ aria-control = 'datetimepicker_dateview']"));selectDate.click ();// nút để di chuyển tiếp theo trong lịchWebElement nextLink = driver.findElement (By.xpath ("// div [@ id = 'datetimepicker_dateview'] // div [@ class = 'k-header'] // a [chứa (@ class, 'k-nav- kế tiếp')]"));// để nhấp vào giữa tiêu đề lịchWebElement midLink = driver.findElement (By.xpath ("// div [@ id = 'datetimepicker_dateview'] // div [@ class = 'k-header'] // a [chứa (@ class, 'k-nav- Nhanh')]"));// nút để di chuyển tháng trước trong lịchWebElement beforeLink = driver.findElement (By.xpath ("// div [@ id = 'datetimepicker_dateview'] // div [@ class = 'k-header'] // a [chứa (@ class, 'k-nav- trước ')] "));// Tách ngày giờ để chỉ lấy phần ngày thángChuỗi date_dd_MM_yyyy [] = (dateTime.split ("") [0]). Split ("/");// lấy chênh lệch năm giữa năm hiện tại và năm để đặt trong calanderint yearDiff = Integer.parseInt (date_dd_MM_yyyy [2]) - Calendar.getInstance (). get (Calendar.YEAR);midLink.click ();if (yearDiff! = 0) {// nếu bạn phải chuyển đi vào năm tớiif (yearDiff> 0) {for (int i = 0; i" + i);nextLink.click ();}}// nếu bạn phải chuyển năm trướckhác nếu (yearDiff <0) {for (int i = 0; i <(yearDiff * (- 1)); i ++) {System.out.println ("Chênh lệch Năm ->" + i);beforeLink.click ();}}}Thread.sleep (1000);// Lấy tất cả các tháng từ lịch để chọn đúngDanh sách list_AllMonthToBook = driver.findElements (By.xpath ("// div [@ id = 'datetimepicker_dateview'] // table // tbody // td [not (contains (@ class, 'k-other-month" ))] "));list_AllMonthToBook.get (Integer.parseInt (date_dd_MM_yyyy [1]) - 1) .click ();Thread.sleep (1000);// lấy tất cả các ngày từ lịch để chọn đúngList list_AllDateToBook = driver.findElements (By.xpath ("// div [@ id = 'datetimepicker_dateview'] // table // tbody // td [not (contains (@ class, 'k-other-month" ))] "));list_AllDateToBook.get (Integer.parseInt (date_dd_MM_yyyy [0]) - 1) .click ();/// CHO THỜI GIANWebElement selectTime = driver.findElement (By.xpath ("// span [@ aria-control = 'datetimepicker_timeview']"));// nhấp vào nút chọn thời gianselectTime.click ();// lấy danh sách thời gianDanh sách allTime = driver.findElements (By.xpath ("// div [@ data-role = 'popup'] [contains (@ style, 'display: block')] // ul // li [@role = 'tùy chọn'] "));dateTime = dateTime.split ("") [1] + "" + dateTime.split ("") [2];// chọn thời gian chính xáccho (WebElement webElement: allTime) {if (webElement.getText (). equalsIgnoreCase (dateTime)){webElement.click ();}}}}
Đầu ra sẽ như thế nào