【问题标题】:Unable to Select & Print of month in a Calendar in Selenium无法在 Selenium 的日历中选择和打印月份
【发布时间】: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

标签: java selenium calendar


【解决方案1】:

有几点需要注意:

  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());

}

输出:

2022 年 11 月

【讨论】:

  • 嗨,感谢您抽出宝贵时间回复我!我有一个问题: ````````````````````````````````````` 字符串 leftMonth_WithYear = wait.until(ExpectedConditions.visibilityOfElementLocated(By. className("bui-calendar__month"))).getText(); String[] G = leftMonth_WithYear.split(" ");字符串 leftNewMonth = G[0];字符串 leftNewYear = G[1]; ````````````````````````````````````````````````在代码中使用这个?? Bcz 我们没有使用正确的??`
  • 是的,这不是必需的,当我编辑您的代码时,我只是更新了该部分。随意删除这些行。
  • 兄弟,我想你错过了这个。请阅读You can you accept this answer so that it will be available in green color for future readers. In case if you would like to accept this answer, please feel free click on check mark which is right aside with voting buttons. I'd appreciate your time and response on this. –
  • 是的,谢谢,只有实践才是学习硒的一种方式。您可以每天尝试大约 2 小时,持续一个月,您将获得足够的知识,无需任何人的帮助即可继续前进。
  • 我从早上开始就坐着..但我真的面临着日历的问题......不同的网站......我感到困惑的是......在这段代码之前:我认为我有很好的练习在 Xpath 中 .. 但是当我检查你的 .. 你是如何用 [2] 创建 Xpath 时......然后它让我想起了 .. 哦!我仍然落后....我还需要在 Xpath 上进行更多练习...以及其他概念... HHahahahaa ....
【解决方案2】:

下面的行指向 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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多