2017-02-23 257 views
-1

我正在使用Java中的Selenium Webdriver自動執行腳本。我想在日期選擇器中選擇一個日期,從當前日期開始+ 2天,我正在使用測試NG框架。如何在日期選擇器中選擇日期從curreny日期開始的2天始終使用硒java

的情況是 - 每當你選擇一個日期時間,應該是當天2天 例如,如果今天是28號,應該選擇30

正確的解決方案是如下 ///取今天的日期通過使用功能java //它將存儲列表中的所有網頁元素 //使用日曆功能在java中添加2日到今天的日期功能 //一旦你有日期使用selenium發送鍵到文本框中選擇(今日+ 2)日期

SimpleDateFormat df = new SimpleDateFormat("MM/dd/YYYY"); 
Date dt = new Date(); 
Calendar cl = Calendar.getInstance(); 
cl.setTime(dt);; 
cl.add(Calendar.DAY_OF_YEAR, 2); 
dt=cl.getTime(); 
String str = df.format(dt); 
    System.out.println("the date today is " + str); 


WebElement el = driver.findElement(By.xpath(".//*[@id='ui-datepicker-div']/table//td")); 
    el.sendKeys(str); 
+4

的可能的複製[使用硒的webdriver選擇從日期選擇器的日期(http://stackoverflow.com/questions/21398575/select-a-date-from-date-picker -using-selenium-webdriver) –

+0

檢查這一個:http://stackoverflow.com/questions/42144111/handle-calender-using-selenium-webdriver/42144270#42144270 – kushal

+0

@Sim檢查了這一點:[如何選擇當前日期+1從日期選擇器彈出,不使用sendkeys](https://stackoverflow.com/questions/41764114/how-to-select-current-date1-from-date-picker-popup-without-using-sendkeys/41767044? noredirect = 1#comment75401998_41767044) – swati

回答

0

基本答案:

DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy"); 
    Calendar cal = Calendar.getInstance(); 
    cal.setTime(new Date()); 
    cal.add(Calendar.DATE, 2); 
    String newDate = dateFormat.format(cal.getTime()); 
相關問題