无法自动检查日期选择器中的日期

huangapple go评论130阅读模式
英文:

Unable to inspect date in datepicker for automation

问题

我正在尝试自动化一个日期选择器,但当我尝试查找单个日期的定位器时,我无法检查它。我已经附上了HTML的屏幕截图,我尝试的代码如下。代码一直运行到选择月份,然后就不起作用了。甚至测试也已经通过,但我不知道如果不选择日期,它怎么能通过。

这是您可以找到日期选择器和HTML定位器代码的链接:https://www.grangehotels.com/

public void datePicker() {
    driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.get("https://www.grangehotels.com/");
//        waitForElementToBeVisible(_coockies, 20);
    clickOnElement(_coockiesHomepage);
    clickOnElement(_bookARoom);
    selectFromDropDownList_ByVisibleText(_selectAHotel, "Grange Buckingham Hotel");
    clickOnElement(_checkinDateHomePage);

    // 选择日期 05/05/2023
    String checkinDate = "05-May-2023";
    String monthYear = "May 2023";
    String date = "5";

    waitForElementToBeVisible(_monthHeading, 50);
    while (true) {
        String monthYearHeading = getTextFromElement(_monthHeading);
        if (monthYearHeading.equals(monthYear)) {
            break;
        } else {
            clickOnElement(_nextMonthArrow);
        }
    }

    List<WebElement> allDates = driver.findElements(By.xpath("//body[1]/div[3]/div[1]/div[2]/table[1]/tbody[1]/tr/td"));

    for (WebElement ele : allDates) {
        String expectedDate = ele.getText();
        if (expectedDate.equals(date)) {
            ele.click();
            break;
        }
    }
}

屏幕截图

英文:

I am trying to automate a datepicker but when I tried to find the locator for an individual date, I'm not able to inspect it. I have attached a screenshot for HTML and the code I have tried is below. Code runs until it selects month, then it does not work. Even test also has passed, I don't know without selecting date how it can pass.

This is the link where you can find the date picker and html codes for locator : https://www.grangehotels.com/

public void datePicker() {
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get(&quot;https://www.grangehotels.com/&quot;);
//        waitForElementToBeVisible(_coockies, 20);
        clickOnElement(_coockiesHomepage);
        clickOnElement(_bookARoom);
        selectFromDropDownList_ByVisibleText(_selectAHotel, &quot;Grange Buckingham Hotel&quot;);
        clickOnElement(_checkinDateHomePage);

        // select date 05/05/2023
        String checkinDate = &quot;05-May-2023&quot;;
        String monthYear = &quot;May 2023&quot;;
        String date = &quot;5&quot;;

        waitForElementToBeVisible(_monthHeading, 50);
        while (true) {
            String monthYearHeading = getTextFromElement(_monthHeading);
            if (monthYearHeading.equals(monthYear)) {
                break;
            } else {
                clickOnElement(_nextMonthArrow);
            }

        }

        List&lt;WebElement&gt; allDates = driver.findElements(By.xpath(&quot;//body[1]/div[3]/div[1]/div[2]/table[1]/tbody[1]/tr/td&quot;));

        for (WebElement ele : allDates) {
            String expectedDate = ele.getText();
            if (expectedDate.equals(date)) {
                ele.click();
                break;
            }
        }
    }

答案1

得分: 0

我已编写了一个帮助方法来设置指定的日期。您在测试中创建了一个LocalDate实例,然后将其传递给帮助方法,它将在日期选择器中设置所需的日期。

在测试中:

String url = "https://www.grangehotels.com/";
driver.get(url);

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#cookieConsent button"))).click();
wait until(ExpectedConditions.elementToBeClickable(By.cssSelector("label.bookingmenu"))).click();
new Select(wait.until(ExpectedConditions.elementToBeClickable(By.id("Hotel")))).selectByVisibleText("Grange Buckingham Hotel");
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[name='check_in_date']")).click();

LocalDate checkInDate = LocalDate.of(2023, Month.MAY, 5);

SetDate(checkInDate);

帮助方法:

public static void SetDate(LocalDate targetDate) {
    // 获取可见的日历表格
    WebDriverWait wait = new WebDriverWait(driver, 10);
    WebElement calendar = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.calendar-table > table"));

    // 移动到目标日期面板
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM yyyy");
    Boolean found = false;
    while (!found) {
        calendar = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.calendar-table > table"));
        String datePanel = calendar.findElement(By.cssSelector("th.month")).getText();

        // 基于目标日期移动右侧
        YearMonth datePanelLocalDate = YearMonth.parse(datePanel, formatter);
        int compareValue = datePanelLocalDate.compareTo(YearMonth.from(targetDate));
        if (compareValue == 0) {
            found = true;
        } else if (compareValue < 0) {
            driver.findElement(By.cssSelector("i.fa-chevron-right")).click();
        }
    }

    // 选择日期
    calendar.findElement(By.xpath(String.format(".//td[@class='available'][text()='%s']", targetDate.getDayOfMonth()))).click();
}

我没有您定义的定位器,所以我编写了自己的。我已在该网站上多次运行此代码,并且一切都按预期工作。

英文:

I've written a helper method to set the specified date. You create a LocalDate instance in the test and then pass that to the helper method and it will set the desired date in the date picker.

In the test

String url = &quot;https://www.grangehotels.com/&quot;;
driver.get(url);

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(&quot;#cookieConsent button&quot;))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(&quot;label.bookingmenu&quot;))).click();
new Select(wait.until(ExpectedConditions.elementToBeClickable(By.id(&quot;Hotel&quot;)))).selectByVisibleText(&quot;Grange Buckingham Hotel&quot;);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(&quot;input[name=&#39;check_in_date&#39;]&quot;))).click();

LocalDate checkInDate = LocalDate.of(2023, Month.MAY, 5);

SetDate(checkInDate);

The helper method

public static void SetDate(LocalDate targetDate) {
	// Get visible calendar TABLE
	WebDriverWait wait = new WebDriverWait(driver, 10);
	WebElement calendar = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(&quot;div.calendar-table &gt; table&quot;)));

	// Move to target date panel
	DateTimeFormatter formatter = DateTimeFormatter.ofPattern(&quot;MMM yyyy&quot;);
	Boolean found = false;
	while (!found) {
		calendar = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(&quot;div.calendar-table &gt; table&quot;)));
		String datePanel = calendar.findElement(By.cssSelector(&quot;th.month&quot;)).getText();

		// move right based on target date
		YearMonth datePanelLocalDate = YearMonth.parse(datePanel, formatter);
		int compareValue = datePanelLocalDate.compareTo(YearMonth.from(targetDate));
		if (compareValue == 0) {
			found = true;
		} else if (compareValue &lt; 0) {
			driver.findElement(By.cssSelector(&quot;i.fa-chevron-right&quot;)).click();
		}
	}

	// Select day
	calendar.findElement(By.xpath(String.format(&quot;.//td[@class=&#39;available&#39;][text()=&#39;%s&#39;]&quot;, targetDate.getDayOfMonth()))).click();
}

I didn't have your defined locators so I wrote my own. I've run this on the site several times and it's working as expected.

huangapple
  • 本文由 发表于 2023年3月7日 23:02:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663660.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定