【发布时间】:2021-10-27 03:19:52
【问题描述】:
因为我是 Selenium 自动化的新手。我正在尝试使用 Calendar 。我的代码在选择 Year & Month 时可以正常工作,但是当我尝试在 table 右侧打印和选择 Month 时它不起作用。当我们点击 booking.com 网站上的日历时,两个月同时出现。一个在左侧,另一个在右侧。它正在正确打印月份值并且能够正确选择月份,直到代码能够在左侧移动月份的值..当我选择了 November 2022 时。这是列表中显示的最后一个月,本月出现在右侧。在这里,我的代码无法打印并选择 11 月的值,但再过几个月,它的打印效果很好……请指导我如何选择并打印出现在日历表右侧的月份值……谢谢你提前....下面我分享我的代码详细信息:-
**
package HandlingCalendars;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class BookingDotCom {
public static void main(String[] args) {
// TODO Auto-generated method stub
String bookingdate = "21-November-2022";
String[] temp = bookingdate.split("-");
String date = temp[0];
String month = temp[1];
String year = temp[2];
System.setProperty("webdriver.chrome.driver", "c:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.get("https://www.booking.com/");
driver.findElement(By.xpath("//div[contains(@data-mode,'checkin')]")).click();
String yearmonth = driver.findElement(By.className("bui-calendar__month")).getText();
String[] G = yearmonth.split(" ");
String newmonth = G[0];
String newyear = G[1];
while(!newyear.equals(year)){
driver.findElement(By.xpath("//div[@class='bui-calendar__control bui-calendar__control--next']")).click();
yearmonth = driver.findElement(By.className("bui-calendar__month")).getText();
G = yearmonth.split(" ");
newmonth = G[0];
newyear = G[1];
}
while(!newmonth.equalsIgnoreCase(month)){
driver.findElement(By.xpath("//div[@class='bui-calendar__control bui-calendar__control--next']")).click();
yearmonth = driver.findElement(By.className("bui-calendar__month")).getText();
G = yearmonth.split(" ");
newmonth = G[0];
newyear = G[1];
}
System.out.println(newmonth);
}
}
**
【问题讨论】:
-
不回答您的问题,请使用
LocalDate作为日期,使用DateTimeFormatter将您的字符串解析为LocalDate。见the tutorial。