英文:
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("https://www.grangehotels.com/");
// waitForElementToBeVisible(_coockies, 20);
clickOnElement(_coockiesHomepage);
clickOnElement(_bookARoom);
selectFromDropDownList_ByVisibleText(_selectAHotel, "Grange Buckingham Hotel");
clickOnElement(_checkinDateHomePage);
// select date 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;
}
}
}
答案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 = "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);
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("div.calendar-table > table")));
// Move to target date panel
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();
// 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 < 0) {
driver.findElement(By.cssSelector("i.fa-chevron-right")).click();
}
}
// Select day
calendar.findElement(By.xpath(String.format(".//td[@class='available'][text()='%s']", 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论