Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用SeleniumWebDriver从日历中选择日期_Java_Selenium_Selenium Webdriver_Webdriver_Webdriverwait - Fatal编程技术网

Java 如何使用SeleniumWebDriver从日历中选择日期

Java 如何使用SeleniumWebDriver从日历中选择日期,java,selenium,selenium-webdriver,webdriver,webdriverwait,Java,Selenium,Selenium Webdriver,Webdriver,Webdriverwait,我是Selenium webdriver的新手。我正在使用Java编程语言 我的问题是我不能调用calendar元素并选择其中的日期 以下是链接: 非常感谢您的帮助 List<WebElement> allDates=driver.findElements(By.xpath("//table[@class='ui-datepicker-calendar']//td")); for(WebElement selectDate:allDates) {

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

我的问题是我不能调用calendar元素并选择其中的日期

以下是链接:

非常感谢您的帮助

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

        }
List allDates=driver.findElements(By.xpath(“//table[@class='ui-datepicker-calendar']//td”);
对于(WebElement selectDate:allDates)
{       
字符串日期=selectDate.getText();
如果(日期等信号情况(“31”))
{
选择日期。单击();
打破
}
}

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

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

您可以使用
startsWith()

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

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

要从日历中为第一个项目选择日期31,您需要为
元素导入WebDriverWait To Eclickable()
,您可以使用以下选项:

  • 代码块:

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


相反,为什么不使用
SendKeys
将日期发送到元素?您可以帮助我们导航到日历吗?创建Webdriverwait实例3次需要什么?@cruisepandey您需要将
20
的演示图替换为测试规范中提到的每个元素的所有3次时间跨度WebDriverWaits是动态的。谢谢@DebanjanB!它确实解决了我的问题。
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();