无法在日历中选择日期 - selenium webdriver

时间:2017-05-02 12:45:44

标签: selenium selenium-webdriver

public class IbiboCalendar {
WebDriver driver;

String selectDepartureDate = "07/25/2017";
@SuppressWarnings("deprecation")
Date selectedDepartureDate = new Date(selectDepartureDate);
SimpleDateFormat simpleDepartureDate = new SimpleDateFormat("MMMM/dd/yyyy");
String formattedDeaparturedate = simpleDepartureDate.format(selectedDepartureDate);

String[] split = formattedDeaparturedate.split("/");
String departureMonth = split[0]+" "+split[2];
String departureDate = split[1];


@FindBy(css = "input[placeholder = 'Choose Date']")
WebElement iDepartDateCalendarBox;



public IbiboCalendar(WebDriver driver){
    this.driver = driver;
    PageFactory.initElements(driver, this);

}

public void goToDepartDateCalendarBox() throws InterruptedException{
    iDepartDateCalendarBox.click();
    Thread.sleep(2000);
    while(true){ 
        try{ 
            driver.findElement(By.xpath("//div[@class = 'DayPicker-Month.DayPicker-Caption'] and [contains(text(),'"+departureMonth+"')]")).isDisplayed();
            driver.findElement(By.xpath("//div[@class = 'DayPicker-Day.calDate'] and [contains(text(),'"+departureDate+"')]")).click();
            break;
        }
        catch(Exception e){
            driver.findElement(By.xpath("//*[@class= 'DayPicker-NavButton DayPicker-NavButton--next']")).click();
            Thread.sleep(1000);
        }
        }



}

}

我通过创建一个对象来调用上面的类来选择一个日期。我面临的问题是,系统一直在点击"下一步"月份中的箭头按钮。我也在xpath选择器语法中出错。请帮助我。

public class IbiboTest {

 static WebDriver driver;

@BeforeClass
public void setUp() throws InterruptedException{
    System.setProperty("webdriver.chrome.driver","C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
    driver = new ChromeDriver();

    //driver= new FirefoxDriver();
    driver.get("https://www.goibibo.com/");
    Thread.sleep(5000);
    driver.manage().window().maximize();

}


@Test
public void testIbiboHomePage() throws InterruptedException{
    IbiboHomePage home = PageFactory.initElements(driver, com.Nalini.Ibibo.IbiboHomePage.class);
    home.clickRoundTripRadioButton();
    home.goToFromSelectionBox();
    Thread.sleep(5000);
    home.goToToSelectionBox();
    Thread.sleep(5000);


    IbiboCalendar calendar = PageFactory.initElements(driver, com.Nalini.Ibibo.IbiboCalendar.class);
    calendar.goToDepartDateCalendarBox();
    Thread.sleep(5000);



}

2 个答案:

答案 0 :(得分:0)

在mozilla浏览器中使用firebug和firepath插件,并使用this链接检查xpath。

答案 1 :(得分:0)

@Nal首先,我想知道您使用哪个日期选择器是Bootstrap或jquery日期选择器。 对于Jquery Picker包含帧,所以你必须切换到它。 下面是Jquery Date picker的代码:

System.setProperty("webdriver.chrome.driver","C:\\Utility\\BrowserDrivers\\chromedriver.exe");
 WebDriver driver=new ChromeDriver();
driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);driver.get("https://jqueryui.com/datepicker/"); 

 driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@class='demo-
 frame'][@src='/resources/demos/datepicker/default.html']")));

driver.findElement(By.id("datepicker")).click();

System.out.println("Datepicker Clicked");