java - 如何在 Eclipse 中使用 Selenium 路径从日历弹出窗口中选择日期

标签 java eclipse selenium calendar

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

弹出窗口的屏幕截图:

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

            }
        }
    }

最佳答案

您可以通过单个 xpath 轻松地做到这一点。由于每个日期都有一个唯一的 ID

a_2017_3_13 which is '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;
    }

完整示例

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

}

关于java - 如何在 Eclipse 中使用 Selenium 路径从日历弹出窗口中选择日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40183029/

相关文章:

java - Eclipse中Java工作集和Resource工作集的区别

java - 跨两个数据库访问数据

android - Eclipse 无法识别引号?

eclipse - 如何使用 SVN 删除与某个模式匹配的所有目录?

java - WebDriver + TestNG - 如何处理测试结果

java - eclipse e4 应用程序中的自定义错误对话框

java - 在 libGDX 中执行操作时如何取消 'Actions."的使用?

c++ - 如何在 Eclipse 和 Linux 上介入代码实现 new()?

c# - 如何通过 Selenium、ChromeDriver 和 GoogleChrome 打开默认 Chrome 配置文件

java - 如何在不使用 jenkins 的情况下在 AWS 中运行本地 Selenium 测试脚本