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

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

Unable to inspect date in datepicker for automation

问题

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

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

  1. public void datePicker() {
  2. driver = new ChromeDriver();
  3. driver.manage().window().maximize();
  4. driver.get("https://www.grangehotels.com/");
  5. // waitForElementToBeVisible(_coockies, 20);
  6. clickOnElement(_coockiesHomepage);
  7. clickOnElement(_bookARoom);
  8. selectFromDropDownList_ByVisibleText(_selectAHotel, "Grange Buckingham Hotel");
  9. clickOnElement(_checkinDateHomePage);
  10. // 选择日期 05/05/2023
  11. String checkinDate = "05-May-2023";
  12. String monthYear = "May 2023";
  13. String date = "5";
  14. waitForElementToBeVisible(_monthHeading, 50);
  15. while (true) {
  16. String monthYearHeading = getTextFromElement(_monthHeading);
  17. if (monthYearHeading.equals(monthYear)) {
  18. break;
  19. } else {
  20. clickOnElement(_nextMonthArrow);
  21. }
  22. }
  23. List<WebElement> allDates = driver.findElements(By.xpath("//body[1]/div[3]/div[1]/div[2]/table[1]/tbody[1]/tr/td"));
  24. for (WebElement ele : allDates) {
  25. String expectedDate = ele.getText();
  26. if (expectedDate.equals(date)) {
  27. ele.click();
  28. break;
  29. }
  30. }
  31. }

屏幕截图

英文:

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/

  1. public void datePicker() {
  2. driver = new ChromeDriver();
  3. driver.manage().window().maximize();
  4. driver.get(&quot;https://www.grangehotels.com/&quot;);
  5. // waitForElementToBeVisible(_coockies, 20);
  6. clickOnElement(_coockiesHomepage);
  7. clickOnElement(_bookARoom);
  8. selectFromDropDownList_ByVisibleText(_selectAHotel, &quot;Grange Buckingham Hotel&quot;);
  9. clickOnElement(_checkinDateHomePage);
  10. // select date 05/05/2023
  11. String checkinDate = &quot;05-May-2023&quot;;
  12. String monthYear = &quot;May 2023&quot;;
  13. String date = &quot;5&quot;;
  14. waitForElementToBeVisible(_monthHeading, 50);
  15. while (true) {
  16. String monthYearHeading = getTextFromElement(_monthHeading);
  17. if (monthYearHeading.equals(monthYear)) {
  18. break;
  19. } else {
  20. clickOnElement(_nextMonthArrow);
  21. }
  22. }
  23. 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;));
  24. for (WebElement ele : allDates) {
  25. String expectedDate = ele.getText();
  26. if (expectedDate.equals(date)) {
  27. ele.click();
  28. break;
  29. }
  30. }
  31. }

答案1

得分: 0

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

在测试中:

  1. String url = "https://www.grangehotels.com/";
  2. driver.get(url);
  3. WebDriverWait wait = new WebDriverWait(driver, 10);
  4. wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#cookieConsent button"))).click();
  5. wait until(ExpectedConditions.elementToBeClickable(By.cssSelector("label.bookingmenu"))).click();
  6. new Select(wait.until(ExpectedConditions.elementToBeClickable(By.id("Hotel")))).selectByVisibleText("Grange Buckingham Hotel");
  7. wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[name='check_in_date']")).click();
  8. LocalDate checkInDate = LocalDate.of(2023, Month.MAY, 5);
  9. SetDate(checkInDate);

帮助方法:

  1. public static void SetDate(LocalDate targetDate) {
  2. // 获取可见的日历表格
  3. WebDriverWait wait = new WebDriverWait(driver, 10);
  4. WebElement calendar = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.calendar-table > table"));
  5. // 移动到目标日期面板
  6. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM yyyy");
  7. Boolean found = false;
  8. while (!found) {
  9. calendar = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.calendar-table > table"));
  10. String datePanel = calendar.findElement(By.cssSelector("th.month")).getText();
  11. // 基于目标日期移动右侧
  12. YearMonth datePanelLocalDate = YearMonth.parse(datePanel, formatter);
  13. int compareValue = datePanelLocalDate.compareTo(YearMonth.from(targetDate));
  14. if (compareValue == 0) {
  15. found = true;
  16. } else if (compareValue < 0) {
  17. driver.findElement(By.cssSelector("i.fa-chevron-right")).click();
  18. }
  19. }
  20. // 选择日期
  21. calendar.findElement(By.xpath(String.format(".//td[@class='available'][text()='%s']", targetDate.getDayOfMonth()))).click();
  22. }

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

英文:

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

  1. String url = &quot;https://www.grangehotels.com/&quot;;
  2. driver.get(url);
  3. WebDriverWait wait = new WebDriverWait(driver, 10);
  4. wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(&quot;#cookieConsent button&quot;))).click();
  5. wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(&quot;label.bookingmenu&quot;))).click();
  6. new Select(wait.until(ExpectedConditions.elementToBeClickable(By.id(&quot;Hotel&quot;)))).selectByVisibleText(&quot;Grange Buckingham Hotel&quot;);
  7. wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(&quot;input[name=&#39;check_in_date&#39;]&quot;))).click();
  8. LocalDate checkInDate = LocalDate.of(2023, Month.MAY, 5);
  9. SetDate(checkInDate);

The helper method

  1. public static void SetDate(LocalDate targetDate) {
  2. // Get visible calendar TABLE
  3. WebDriverWait wait = new WebDriverWait(driver, 10);
  4. WebElement calendar = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(&quot;div.calendar-table &gt; table&quot;)));
  5. // Move to target date panel
  6. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(&quot;MMM yyyy&quot;);
  7. Boolean found = false;
  8. while (!found) {
  9. calendar = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(&quot;div.calendar-table &gt; table&quot;)));
  10. String datePanel = calendar.findElement(By.cssSelector(&quot;th.month&quot;)).getText();
  11. // move right based on target date
  12. YearMonth datePanelLocalDate = YearMonth.parse(datePanel, formatter);
  13. int compareValue = datePanelLocalDate.compareTo(YearMonth.from(targetDate));
  14. if (compareValue == 0) {
  15. found = true;
  16. } else if (compareValue &lt; 0) {
  17. driver.findElement(By.cssSelector(&quot;i.fa-chevron-right&quot;)).click();
  18. }
  19. }
  20. // Select day
  21. calendar.findElement(By.xpath(String.format(&quot;.//td[@class=&#39;available&#39;][text()=&#39;%s&#39;]&quot;, targetDate.getDayOfMonth()))).click();
  22. }

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:

确定