无法在 Selenium 的日历中 Select & 打印月份

Unable to Select & Print of month in a Calendar in Selenium

因为我是 Selenium Automation 的新手。我正在尝试 Calendar 。我的代码在 selecting Year & Month 上正常工作,但是当我尝试在 table 的右侧打印 & selecting Month 时它不起作用。当我们点击 booking.com 网站上的日历时,两个月会同时出现。一个在左侧,另一个在右侧。它正在正确打印月份值并且能够正确地 select 月份,直到代码能够在左侧移动月份的值。当我 select 编辑 2022 年 11 月时。这是列表中显示的最后一个月,本月显示在右侧。这里我的代码无法打印 & select 11 月的值,但在接下来的几个月里它打印正常......请指导我如何 select & 打印出现在右侧的月份值日历 table ...提前谢谢你....下面我分享我的代码详细信息:-


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

}

**

有几点需要注意:

  1. 使用正确的定位符。
  2. 处理动态内容时使用显式等待。

示例代码:

@Test
public void testSO() throws InterruptedException, MalformedURLException {

String bookingdate = "21-November-2022";
String[] temp = bookingdate.split("-");
String date = temp[0];
String month = temp[1];
String year = temp[2];

driver.manage().window().maximize();
driver.get("https://www.booking.com/");
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[contains(@data-mode,'checkin')]"))).click();

String leftMonth_WithYear = wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("bui-calendar__month"))).getText();
String[] G = leftMonth_WithYear.split(" ");
String leftNewMonth = G[0];
String leftNewYear = G[1];

while(true){
    if(driver.findElement(By.xpath("(//div[@class='bui-calendar__month'])[2]")).getText().split("\ ")[0].equals(month) && driver.findElement(By.xpath("(//div[@class='bui-calendar__month'])[2]")).getText().split("\ ")[1].equals(year)) {
        break;
    }
    else {
        wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[contains(@data-bui-ref, 'calendar-next')]//*[name()='svg']"))).click();
        Thread.sleep(500);
    }
}

System.out.println(wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("(//div[@class='bui-calendar__month'])[2]"))).getText());

}

输出:

November 2022

下一行指向 DOM 中的 2 个元素 - October 2022November 2022.

yearmonth = driver.findElement(By.className("bui-calendar__month")).getText();

所以默认情况下它选择第一个选项 October 2022

使用此 xpath 而不是 class_name 来获取右侧 month 的文本。

//div[@class='bui-calendar__wrapper'][2]/div

或更改

yearmonth = driver.findElement(By.className("bui-calendar__month")).getText();

months = driver.findElements(By.className("bui-calendar__month"));
yearmonth = months[1].getText();