如何使用 selenium webdriver select 从日历中获取日期

How to select date from calendar using selenium webdriver

我是 Selenium webdriver 的新手。我正在使用 Java 编程语言。

我的问题是我无法调用日历元素和 select 其中的日期。

这里是link:

https://www.veltra.com/en/asia/malaysia/kuala_lumpur/a/139387

非常感谢任何帮助。

List<WebElement> allDates=driver.findElements(By.xpath("//table[@class='ui-datepicker-calendar']//td"));

        for(WebElement selectDate:allDates)
        {       
            String date=selectDate.getText();

            if(date.equalsIgnoreCase("31"))
            {
                selectDate.click();
                break;
            }

        }

我的计划是,点击"Book Now"按钮后,我想在日历中select 31st July。然后日期将显示在文本框中。

活动日期的文字不仅是日期,还包含价格。例如,7 月 31 日的文本是 31\nUSD 266.67.

您可以使用 startsWith()

if (date.startsWith("31")) {
    selectDate.click();
    break;
}

或者使用指向日期本身的定位器

By.xpath("//table[@class='ui-datepicker-calendar']//td//span")

到 select 日期 31 来自日历的第一个项目你需要为 [=] 引入 WebDriverWait 11=] 并且您可以使用以下 :

  • 代码块:

    WebDriver driver = new ChromeDriver(options);
    driver.get("https://www.veltra.com/en/asia/malaysia/kuala_lumpur/a/139387");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='hltitle clearfix fclear price_note_wrapper']//following::a[2]"))).click();
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='hltitle clearfix fclear price_note_wrapper']//following::a[starts-with(@id, 'book_now_btn_')][1]//following::table[1]//input[starts-with(@name, 'data')]"))).click();
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//table[@class='ui-datepicker-calendar']//span[text()='31']"))).click();
    
  • 浏览器快照: