<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 如何從 Selenium Webdriver 中的 DatePicker /日歷中選擇日期 > 原文: [https://www.guru99.com/handling-date-time-picker-using-selenium.html](https://www.guru99.com/handling-date-time-picker-using-selenium.html) 對于 DateTime 選擇,HTML5 具有如下所示的新控件。 ![How to Select Date from DatePicker in Selenium Webdriver](https://img.kancloud.cn/6a/be/6abe9d82e7ff2cdd67b2170a8f680ae1_468x165.png "Handling Date Time Picker using Selenium") 可以在此處訪問上面的頁面[](http://demo.guru99.com/test/) 如果看到 DateTime Picker 控件的 DOM,則日期和時間都只有一個輸入框。 ![How to Select Date from DatePicker in Selenium Webdriver](https://img.kancloud.cn/87/5c/875cefa9da0a2ce3defcae29d7c377ce_549x343.png "Handling Date Time Picker using Selenium") 因此,要首先處理這種類型的控件,我們將填充日期而不用定界符分隔,即如果日期為 2013 年 9 月 25 日,則將 09252013 傳遞到輸入框。 完成后,我們將通過按“制表符” &填充時間將焦點從日期轉移到時間。 如果需要填充 02:45 PM,則將其“ 0245PM”傳遞給同一輸入框。 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("http://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"); } } ``` 輸出將像- ![How to Select Date from DatePicker in Selenium Webdriver](https://img.kancloud.cn/4d/86/4d863f7d19bc9f7a7c36e0acee005038_450x245.png "Handling Date Time Picker using Selenium") 讓我們看一下另一個 Calendar 示例。 我們將使用 Telerik DateTimePicker 控件。 可以在處訪問[](http://demos.telerik.com/kendo-ui/datetimepicker/index) ![How to Select Date from DatePicker in Selenium Webdriver](https://img.kancloud.cn/aa/0c/aa0c1a810359337478009faa7671707e_624x245.png "Handling Date Time Picker using Selenium") 如果需要更改月份,則必須單擊日歷標題的中間位置。 ![How to Select Date from DatePicker in Selenium Webdriver](https://img.kancloud.cn/85/c1/85c1ba9ace4b2ea9cc1b215d8faeec1b_501x352.png "Handling Date Time Picker using Selenium") 同樣,如果需要更改年份,則可以通過單擊日期選擇器上的下一個或上一個鏈接來完成。 ![How to Select Date from DatePicker in Selenium Webdriver](https://img.kancloud.cn/f0/d1/f0d1eee86adc72d18370cd4b4a0f041d_512x347.png "Handling Date Time Picker using Selenium") 最后,要更改時間,我們可以從下拉菜單中選擇正確的時間(注意:此處的時間間隔為 30 分鐘,即 12:00、12:30、1:00、1:30 等)。 ![How to Select Date from DatePicker in Selenium Webdriver](https://img.kancloud.cn/e3/f1/e3f1c0457283f1b941937f9068444688_410x295.png "Handling Date Time Picker using Selenium") 一個完整的例子看起來像- ``` 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("http://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(); } } } } ``` 輸出會像 ![How to Select Date from DatePicker in Selenium Webdriver](https://img.kancloud.cn/36/c3/36c35d7016be0e5025b40c61ae5f895c_562x219.png "Handling Date Time Picker using Selenium")
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看