Πώς να επιλέξετε την ημερομηνία από το DatePicker/Ημερολόγιο στο Selenium Πρόγραμμα οδήγησης Web
Πώς να χειριστείτε το Ημερολόγιο Selenium
Για την επιλογή DateTime, το HTML5 έχει ένα νέο στοιχείο ελέγχου που φαίνεται παρακάτω.
Η πρόσβαση στην παραπάνω σελίδα είναι δυνατή εδώ: https://demo.guru99.com/test/
Εάν δούμε το DOM του στοιχείου ελέγχου Επιλογής ημερομηνίας ώρας, θα υπάρχει μόνο ένα πλαίσιο εισαγωγής τόσο για την ημερομηνία όσο και για την ώρα.
Έτσι, για να χειριστούμε αυτό το είδος ελέγχου πρώτα θα συμπληρώσουμε την ημερομηνία χωρίς διαχωρισμό με οριοθέτη, δηλαδή εάν η ημερομηνία είναι 09/25/2013, τότε θα περάσουμε το 09252013 στο πλαίσιο εισαγωγής. Μόλις τελειώσουμε, θα αλλάξουμε την εστίαση από ημερομηνία σε ώρα πατώντας «καρτέλα» και χρόνος πλήρωσης.
Εάν πρέπει να συμπληρώσουμε τις 02:45 μ.μ. , θα περάσουμε ένα '0245 PM' στο ίδιο πλαίσιο εισαγωγής.
Ο κωδικός για το datepicker μοιάζει με αυτό -
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class DateTimePicker {
@Test
public void dateTimePicker(){
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://demo.guru99.com/test/");
//Find the date time picker control
WebElement dateBox = driver.findElement(By.xpath("//form//input[@name='bdaytime']"));
//Fill date as mm/dd/yyyy as 09/25/2013
dateBox.sendKeys("09252013");
//Press tab to shift focus to time field
dateBox.sendKeys(Keys.TAB);
//Fill time as 02:45 PM
dateBox.sendKeys("0245PM");
}
}
Η έξοδος θα είναι σαν-
Ας δούμε ένα άλλο παράδειγμα Ημερολογίου. Θα χρησιμοποιήσουμε το στοιχείο ελέγχου Telerik DateTimePicker. Είναι δυνατή η πρόσβαση εδώ
Εδώ, αν χρειαστεί να αλλάξουμε τον μήνα, πρέπει να κάνουμε κλικ στη μέση της κεφαλίδας του ημερολογίου.
Ομοίως, αν χρειαστεί να αλλάξουμε το έτος, μπορούμε να το κάνουμε κάνοντας κλικ στον επόμενο ή προηγούμενους συνδέσμους στον επιλογέα ημερομηνίας.
Και τέλος για την αλλαγή της ώρας μπορούμε να επιλέξουμε τη σωστή ώρα από το αναπτυσσόμενο μενού (Σημείωση: Εδώ η ώρα επιλέγεται σε κενό 30 λεπτών, π.χ., 12:00, 12:30 , 1:00, 1:30 κ.λπ.).
Ένα πλήρες παράδειγμα μοιάζει με-
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class DatePicker {
@Test
public void testDAtePicker() throws Exception{
//DAte and Time to be set in textbox
String dateTime ="12/07/2014 2:00 PM";
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://demos.telerik.com/kendo-ui/datetimepicker/index");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//button to open calendar
WebElement selectDate = driver.findElement(By.xpath("//span[@aria-controls='datetimepicker_dateview']"));
selectDate.click();
//button to move next in calendar
WebElement nextLink = driver.findElement(By.xpath("//div[@id='datetimepicker_dateview']//div[@class='k-header']//a[contains(@class,'k-nav-next')]"));
//button to click in center of calendar header
WebElement midLink = driver.findElement(By.xpath("//div[@id='datetimepicker_dateview']//div[@class='k-header']//a[contains(@class,'k-nav-fast')]"));
//button to move previous month in calendar
WebElement previousLink = driver.findElement(By.xpath("//div[@id='datetimepicker_dateview']//div[@class='k-header']//a[contains(@class,'k-nav-prev')]"));
//Split the date time to get only the date part
String date_dd_MM_yyyy[] = (dateTime.split(" ")[0]).split("/");
//get the year difference between current year and year to set in calander
int yearDiff = Integer.parseInt(date_dd_MM_yyyy[2])- Calendar.getInstance().get(Calendar.YEAR);
midLink.click();
if(yearDiff!=0){
//if you have to move next year
if(yearDiff>0){
for(int i=0;i< yearDiff;i++){
System.out.println("Year Diff->"+i);
nextLink.click();
}
}
//if you have to move previous year
else if(yearDiff<0){
for(int i=0;i< (yearDiff*(-1));i++){
System.out.println("Year Diff->"+i);
previousLink.click();
}
}
}
Thread.sleep(1000);
//Get all months from calendar to select correct one
List<WebElement> 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);
//get all dates from calendar to select correct one
List<WebElement> 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();
///FOR TIME
WebElement selectTime = driver.findElement(By.xpath("//span[@aria-controls='datetimepicker_timeview']"));
//click time picker button
selectTime.click();
//get list of times
List<WebElement> allTime = driver.findElements(By.xpath("//div[@data-role='popup'][contains(@style,'display: block')]//ul//li[@role='option']"));
dateTime = dateTime.split(" ")[1]+" "+dateTime.split(" ")[2];
//select correct time
for (WebElement webElement : allTime) {
if(webElement.getText().equalsIgnoreCase(dateTime))
{
webElement.click();
}
}
}
}
Η έξοδος θα είναι σαν








