【问题标题】:How to select date from calendar using selenium webdriver如何使用 selenium webdriver 从日历中选择日期
【发布时间】:2019-07-11 07:03:35
【问题描述】:

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

我的问题是我无法调用日历元素并选择其中的日期。

这里是链接:

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

        }

我的计划是,点击“立即预订”按钮后,我想在日历中选择31st July。然后日期将显示在文本框中。

【问题讨论】:

  • 相反,为什么不使用SendKeys 将日期发送到元素?
  • 您能帮我们导航到日历吗?

标签: java selenium selenium-webdriver webdriver webdriverwait


【解决方案1】:

要从日历中选择日期31,您需要为elementToBeClickable() 诱导WebDriverWait,您可以使用以下Locator Strategies

  • 代码块:

    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();
    
  • 浏览器快照:

【讨论】:

  • 3次创建Webdriverwait实例需要什么?
  • @cruisepandey 您需要将 20 的演示图替换为 Test Specification 中提到的每个单独元素的时间跨度,因为所有 3 个 WebDriverWaits i> 是动态的。
  • 感谢@DebanjanB! WebDriverWait 绝对解决了我的问题。
【解决方案2】:

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

您可以使用startsWith()

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-19
    • 2021-09-09
    • 2023-03-18
    • 1970-01-01
    • 2019-03-22
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多