如何在eclipse中使用selenium路径从日历弹出窗口中选择日期

时间:2016-10-21 18:13:42

标签: java eclipse selenium calendar

我正在尝试从弹出的日历中选择出发日期和返回日期,但发现很难为日期选择编写通用代码。我正在尝试编写一个方法,其中日期从main方法作为参数和方法传递将执行并单击日历弹出窗口并选择单击日期。我已经编写代码直到找到月份但在此之后我被困在日期路径中请帮助。

弹出的屏幕截图:

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();

            }
        }
    }

2 个答案:

答案 0 :(得分:1)

您可以通过单个xpath轻松完成此操作。由于每个日期都有唯一的ID

  

a_2017_3_13这是&#39; a_year_month_day&#39;

您可以直接构建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;
    }

完整样本

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();
    }

}

答案 1 :(得分: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();
    }
}