2016-10-21 1 views
0

캘린더 팝업에서 출발일과 반환 날짜를 선택하려고하지만 날짜 선택을위한 제네릭 코드를 작성하기가 어렵습니다. 어떤 날짜가 main 메소드에서 인수로 전달되고 메서드가 실행되고 캘린더 팝업을 클릭하고 date를 클릭하여 선택합니다. 나는 그 달을 찾을 때까지 코드를 작성했지만 그 후에는 날짜 경로에 붙어 있습니다. 팝업의이클립스에서 셀렌 경로를 사용하여 캘린더 팝업에서 날짜를 선택하는 방법

스크린 샷 : 내가 사용하고

enter image description here

웹 사이트는 https://www.yatra.com/

Here is my code: 



package Website; 

    import java.util.Date; 
    import java.util.List; 
    import java.util.concurrent.TimeUnit; 

    import org.openqa.selenium.By; 
    import org.openqa.selenium.Keys; 
    import org.openqa.selenium.NoSuchElementException; 
    import org.openqa.selenium.WebDriver; 
    import org.openqa.selenium.WebElement; 
    import org.openqa.selenium.firefox.FirefoxDriver; 
    import org.openqa.selenium.interactions.Actions; 

    public class PickDateCalender { 
     static WebDriver driver=new FirefoxDriver();; 


     public static void main(String[] args) throws InterruptedException { 



      driver.get("https://www.yatra.com/"); 

      driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); 
      WebElement element=driver.findElement(By.className("dropdown-toggle")); 

      String ori="New Delhi, India (DEL)"; 
      String dest="Bangalore, India (BLR)"; 

      String DepartDte="23-October-2016"; 
      String splitter[]=DepartDte.split("-"); 
      String Departdate=splitter[0]; 
      System.out.println("date"+" "+Departdate); 
      String Departmonth=splitter[1]; 
      System.out.println("month"+" "+Departmonth); 
      String Departyear=splitter[2]; 
      System.out.println("year"+" "+Departyear); 
      String returDte=""; 
      ; 
      selectDate(Departdate,Departmonth,Departyear); 

     } 



public static void selectDate(String Depardate,String Departmonth,String Departyear){ 
     WebElement Depart=driver.findElement(By.id("BE_flight_depart_date")); 
     Depart.click(); 
     List <WebElement> month =driver.findElements(By.xpath(".//*[@id='PegasusCal-0']//ul[@class='month-list']")); 
     for(int i=0;i<month.size();i++){ 
      String monname=month.get(i).getText(); 


      if(monname.contains(Departmonth)){ 
       System.out.println("Match found"+" "+monname); 
       System.out.println("inside if"); 
       month.get(i).click(); 

       break; 


       } 
       driver.close(); 

      } 
     } 
    } 
+0

당신이 원하는 것 –

+0

괜찮아요. 내가 쿼리 plzz revertr을 check.I 업데이 트됩니다 내 쿼리를 업데이 트가 충분히 좋은거야 ?? –

+0

http://seleniumtutorialpoint.com/2015/02/how-to-handle-calendar-pop-up-in-selenium-webdriver/ 및 http://www.mythoughts.co.in/2013/04를 참조하십시오. /selecting-date-from-datepicker-using.html#.WApfSisRCUI 및 http://www.guru99.com/handling-date-time-picker-using-selenium.html –

답변

1

여기를 클릭 당신이 할 쉽게 할 수있는 단일의 XPath에 의한. 각 날짜는 고유 ID를 'a_year_month_day'

당신은 직접 XPath를 구축하고 그것을 할 수

..

private void selectDate(String Departdate, String Departmonth, String Departyear) { 
     //div[@id='PegasusCal-0']//a[@id='a_2017_3_13'] 
     String dateXpath = String.format(
       "//div[@id='PegasusCal-0']//a[@id='a_%s_%d_%s']", 
       Departyear, getMonthNum(Departmonth), Departdate); 
     driver.findElement(By.xpath(dateXpath)).click(); 
    } 

    //As you are passing input in name 'October' parsing that to number 
    private int getMonthNum(String monthName) { 
     try { 
      Date date = new SimpleDateFormat("MMM").parse(monthName); 
      Calendar cal = Calendar.getInstance(); 
      cal.setTime(date); 
      return cal.get(Calendar.MONTH) + 1; 
     } catch (ParseException ex) { 
      Logger.getLogger(Yatra.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     return 1; 
    } 

전체 샘플은

a_2017_3_13이 있기 때문에

import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import java.util.Date; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.chrome.ChromeDriver; 

/** 
* 
* @author Phystem 
*/ 
public class Yatra { 

    WebDriver driver; 

    public Yatra() { 
     System.setProperty("webdriver.chrome.driver", "D:\\Test\\chromedriver.exe"); 
     driver = new ChromeDriver(); 
    } 

    public void start() { 
     driver.get("https://www.yatra.com/"); 
     String DepartDte = "29-March-2017"; 
     String splitter[] = DepartDte.split("-"); 
     String Departdate = splitter[0]; 
     System.out.println("date" + " " + Departdate); 
     String Departmonth = splitter[1]; 
     System.out.println("month" + " " + Departmonth); 
     String Departyear = splitter[2]; 
     System.out.println("year" + " " + Departyear); 
     String returDte = ""; 
     driver.findElement(By.name("flight_depart_date")).click(); 
     selectDate(Departdate, Departmonth, Departyear); 
    } 

    private void selectDate(String Departdate, String Departmonth, String Departyear) { 
     //div[@id='PegasusCal-0']//a[@id='a_2017_3_13'] 
     String dateXpath = String.format(
       "//div[@id='PegasusCal-0']//a[@id='a_%s_%d_%s']", 
       Departyear, getMonthNum(Departmonth), Departdate); 
     driver.findElement(By.xpath(dateXpath)).click(); 
    } 

    private int getMonthNum(String monthName) { 
     try { 
      Date date = new SimpleDateFormat("MMM").parse(monthName); 
      Calendar cal = Calendar.getInstance(); 
      cal.setTime(date); 
      return cal.get(Calendar.MONTH) + 1; 
     } catch (ParseException ex) { 
      Logger.getLogger(Yatra.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     return 1; 
    } 

    public static void main(String[] args) { 
     new Yatra().start(); 
    } 

} 
+0

와우가 작동합니다! 고마워. 셀레늄을 처음 접했으니 여기에서 사용한 모든 복잡한 메소드를 알아 내려고 노력하고있다.이 코드를 내 이해와이 코드가 잘 풀렸다. 잘 살펴봐. –

+0

아래에서 해결 된 코드를 게시하고 다시 도움을 요청 해 주셔서 감사합니다 :) –

0
package Website; 

import java.util.Date; 
import java.util.List; 
import java.util.concurrent.TimeUnit; 

import org.openqa.selenium.By; 
import org.openqa.selenium.Keys; 
import org.openqa.selenium.NoSuchElementException; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.interactions.Actions; 

public class PickDateCalender { 
    static WebDriver driver=new FirefoxDriver();; 
public static void main(String[] args) throws InterruptedException { 
driver.get("https://www.yatra.com/"); 
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); 
    WebElement element=driver.findElement(By.className("dropdown-toggle")); 

     String ori="New Delhi, India (DEL)"; 
     String dest="Bangalore, India (BLR)"; 

     String DepartDte="23-November-2016"; 
     String splitter[]=DepartDte.split("-"); 
     String Departdate=splitter[0]; 
     String Departmonth=splitter[1]; 
     String Departyear=splitter[2]; 
     selectDate(Departdate,Departmonth,Departyear); 

    } 

    public static void selectDate(String Departdate,String Departmonth,String Departyear){ 
     WebElement Depart=driver.findElement(By.id("BE_flight_depart_date")); 
     Depart.click(); 
     List <WebElement> month =driver.findElements(By.xpath(".//*[@id='PegasusCal-0']//ul[@class='month-list']/li/a")); 
     for(int i=0;i<month.size();i++){ 
      String monname=month.get(i).getText(); 
      System.out.println(monname); 

      if(monname.equals(Departmonth)){ 
       System.out.println("Match found"+" "+monname); 
       System.out.println("inside if"); 
       month.get(i).click(); 
       List<WebElement> dte= driver.findElements(By.xpath("//div[@class='cal-body']/div[2]/div/div/div[2][@class='month-box']//tbody/tr/td/a")); 
       for (WebElement cell:dte){ 

        System.out.println(cell.getText()); 
        if (cell.getText().equals(Departdate)){ 
         System.out.println(cell.getText()); 
         cell.click(); 
         break; 

        } 


       } 
       break; 






      } 


     } 

     //driver.close(); 
    } 
} 
관련 문제