如何在Selenium WebDriver中选择日期选择器 [英] How to select the Date Picker In Selenium WebDriver

查看:1274
本文介绍了如何在Selenium WebDriver中选择日期选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前正在使用 Selenium WebDriver 并使用 Java 。我想从下拉列表中选择日期范围中的值。我想知道如何选择值日期,月份和年份在日期选择器下拉列表中。

Currently working on Selenium WebDriver and using Java. I want to select values in date range from the drop down.. I want to know how can I select the values as Date, Month and year in the date picker drop down.

这是HTML标记:

<dd id="date-element">
<input id="fromDate" class="hasDatepicker" type="text" style="width:57px; padding:3px 1px; font-size:11px;" readonly="readonly" name="fromDate" value="01 Jan 2013">

<input id="toDate" class="hasDatepicker" type="text" style="width:57px; padding:3px 1px; font-size:11px;" readonly="readonly" name="toDate" value="31 Dec 2013">  

我尝试过以下示例代码:

The below sample code i tried:

Log.info("Clicking on From daterange dropdown");
JavascriptExecutor executor8 = (JavascriptExecutor)driver;
executor8.executeScript("document.getElementById('fromDate').style.display='block';");
Select select8 = new Select(driver.findElement(By.id("fromDate")));
select8.selectByVisibleText("10 Jan 2013");
Thread.sleep(3000);

Log.info("Clicking on To daterange dropdown");
JavascriptExecutor executor10 = (JavascriptExecutor)driver;
executor10.executeScript("document.getElementById('toDate').style.display='block';");
Select select10 = new Select(driver.findElement(By.id("toDate")));
select10.selectByVisibleText("31 Dec 2013");
Thread.sleep(3000);


推荐答案

DatePicker不是选择元素。你在代码中所做的是错误的。

DatePicker are not Select element. What your doing in your code is wrong.

Datepicker实际上是一组包含行和列的表。要选择一个日期,您只需导航到我们所需日期所在的单元格。

Datepicker are in fact table with set of rows and columns.To select a date you just have to navigate to the cell where our desired date is present.

所以你的代码应该是这样的:

So your code should be like this:

WebElement dateWidget = driver.findElement(your locator);
List<WebElement> columns=dateWidget.findElements(By.tagName("td"));

for (WebElement cell: columns){
   //Select 13th Date 
   if (cell.getText().equals("13")){
      cell.findElement(By.linkText("13")).click();
      break;
 }

这篇关于如何在Selenium WebDriver中选择日期选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆