我如何在 Selenium Date Picker 中处理这个日历 Scenrio

How do i handle this calender Scenrio in the Selenium Date Picker

提问人:Krishna Sriram 提问时间:10/16/2023 更新时间:10/16/2023 访问量:23

问:

在此代码中,我想导航到 2022 年并选择 7 月并在控制台中显示输出,但问题是在检查时的 else if 条件中 当计算此特定条件时,年份是 2022 年,月份是 12 月,它正在过去并且它正在导航到下一个按钮,即 2023 年 1 月,因此循环继续运行,我无法获得我想要的结果

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class DatePicker {
    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5000));

        driver.get("https://seleniumpractise.blogspot.com/2016/08/how-to-handle-calendar-in-selenium.html");

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

        String desiredMonth = "July";
        String desiredYear = "2022";

        while (true) {
            String year1 = driver.findElement(By.className("ui-datepicker-year")).getText();
            String month1 = driver.findElement(By.className("ui-datepicker-month")).getText();

            if (year1.equals(desiredYear) && month1.equalsIgnoreCase(desiredMonth)) {
                System.out.println("Desired date reached: the year is " + year1 + " and the month is " + month1);
                break;
            } else if (year1.equals(desiredYear) && month1.compareTo(desiredMonth) < 0) {
                driver.findElement(By.className("ui-icon-circle-triangle-e")).click();
            } else {
                driver.findElement(By.className("ui-icon-circle-triangle-w")).click();
            }

            // Wait for the changes to take effect
            wait.until(ExpectedConditions.presenceOfElementLocated(By.className("ui-datepicker-year")));
        }

        driver.quit();
    }
}

    


    
Java Selenium-Web驱动程序 测试

评论


答:

0赞 Akshit Gupta 10/16/2023 #1

您面临的问题可能是由于“else if”条件中的逻辑造成的。当您检查年份是否为 2022 年并且月份小于“7 月”时,它没有正确考虑 12 月。 添加另一个条件来处理这种情况。

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class DatePicker {
  public static void main(String[] args) {
    WebDriver driver = new ChromeDriver();
    WebDriverWait wait = new WebDriverWait(driver, 
    Duration.ofSeconds(5000));

    driver.get("https://seleniumpractise.blogspot.com/2016/08/how-to-handle-calendar-in-selenium.html");

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

    String desiredMonth = "July";
    String desiredYear = "2022";

    while (true) {
        String year1 = driver.findElement(By.className("ui-datepicker-year")).getText();
        String month1 = driver.findElement(By.className("ui-datepicker-month")).getText();

        if (year1.equals(desiredYear) && month1.equalsIgnoreCase(desiredMonth)) {
            System.out.println("Desired date reached: the year is " + year1 + " and the month is " + month1);
            break;
        } else if (year1.equals(desiredYear) && month1.compareTo(desiredMonth) < 0) {
            driver.findElement(By.className("ui-icon-circle-triangle-e")).click();
        } else if (year1.equals(desiredYear) && month1.compareTo(desiredMonth) > 0) {
            driver.findElement(By.className("ui-icon-circle-triangle-w")).click();
        } else {
            // If year1 is not equal to desiredYear, go forward
            driver.findElement(By.className("ui-icon-circle-triangle-e")).click();
        }

        // Wait for the changes to take effect
        wait.until(ExpectedConditions.presenceOfElementLocated(By.className("ui-datepicker-year")));
    }

    driver.quit();
 }
}