简体   繁体   English

硒日历:月和日期选择:我在做什么错

[英]Selenium Calendar: Months and Dates Selections: What i'm doing wrong

i'm trying to select Months and Dates from a calendar (www.booking.com) for practice purposes but i can not get it to select the month, if the month is into left panel. 我试图出于练习目的从日历(www.booking.com)中选择月份和日期,但是如果月份在左侧面板中,则我无法选择月份和日期。 Probably i'm missing something. 可能我缺少了一些东西。 Can anyone give me a hint? 谁能给我一个提示? or make an assist, it would be much appreciated. 或提供协助,将不胜感激。 Thanks in advance. 提前致谢。

My Code: 我的代码:

   public void calendar() throws InterruptedException {


        String selectDate = "6/11/2020";
        Date d = new Date(selectDate);

        SimpleDateFormat years = new SimpleDateFormat("yyyy");
        SimpleDateFormat months = new SimpleDateFormat("MMMM");
        SimpleDateFormat days = new SimpleDateFormat("d");
        String year = years.format(d);
        String month = months.format(d);
        String day = days.format(d);
        String gap = " ";
        String search = month + gap + year;


        while (!driver.findElement(By.xpath("//div[@class='xp-calendar']/div/div/div/div/*[contains(@class,'bui-calendar__month')]")).getText().equalsIgnoreCase(search)) {
            Thread.sleep(1000);
            driver.findElement(By.xpath("//div[@class='xp-calendar']/div/div/div[2]")).click();
        }


        int coutDays = driver.findElements(By.xpath("//div[@class='xp-calendar']/div/div/div/div/table/tbody/tr/td")).size();

        for (int i = 0; i < coutDays; i++) {
            String searchingDay = driver.findElements(By.xpath("//div[@class='xp-calendar']/div/div/div/div/table/tbody/tr/td")).get(i).getText();
            if (searchingDay.equalsIgnoreCase(day)) {
                Thread.sleep(1000);
                driver.findElements(By.xpath("//div[@class='xp-calendar']/div/div/div/div/table/tbody/tr/td")).get(i).click();
                break;
            }

        }

you can use the css to select the calendar date. 您可以使用CSS选择日历日期。

driver.findElement(By.cssSelector("td[data-date='2019-03-21']")).click();

Make sure you pass the date in "YYYY-MM-DD" format. 确保以“ YYYY-MM-DD”格式传递日期。

Here is the code if you want to try in your console first. 如果您想首先在控制台中尝试,请使用以下代码。

document.querySelector('td[data-date="2019-03-21"]').click()

You don't have to open the date picker, just navigate to the page and run the above. 您不必打开日期选择器,只需导航到页面并运行上面的命令即可。

Correct Code: 正确的代码:

String date = "10-June 2020";
String splitter[]= date.split("-");
String checkInMonth_Year = splitter[1];
String checkInDay = splitter[0];


List<WebElement> a = driver.findElements(By.xpath("//div[@class='bui-calendar']/div/div/div/div"));

for (int i=0; i<a.size(); i++)
{
    System.out.println(a.get(i).getText());
    if (a.get(i).getText().equals(checkInMonth_Year))
    {
        List<WebElement> days = driver.findElements(By.xpath("//div[@class='bui-calendar']/div/div/div["+(i+1)+"]/table/tbody/tr/td[@class='bui-calendar__date']"));
        for (WebElement d:days)
        {
            if (d.getText().equals(checkInDay))
            {
                d.click();
                return;
            }
        }


    }
}
pauseFor1Sec();
driver.findElement(By.xpath("//div[contains(@class,'bui-calendar__control bui-calendar__control--next')]")).click();
calendar();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM