gpt4 book ai didi

java - Selenium 日历: Months and Dates Selections: What i'm doing wrong

转载 作者:行者123 更新时间:2023-12-02 10:10:13 34 4
gpt4 key购买 nike

我正在尝试从日历(www.booking.com)中选择月份和日期以进行练习,但如果月份位于左侧面板中,我无法选择月份。可能我错过了一些东西。谁能给我提示吗?或提供协助,将不胜感激。提前致谢。

我的代码:

   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;
}

}

最佳答案

您可以使用CSS来选择日历日期。

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

确保以“YYYY-MM-DD”格式传递日期。

如果您想先在控制台中尝试,请使用以下代码。

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

您不必打开日期选择器,只需导航到该页面并运行上面的代码即可。

关于java - Selenium 日历: Months and Dates Selections: What i'm doing wrong,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55054033/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com