[英]Not able to select date using java script executor in Selenium webdriver
I want to select a particular date using JavaScript executor but am not able to select and I also didn't get any execution.我想使用 JavaScript 执行程序选择特定日期,但无法选择,而且我也没有得到任何执行。
WebDriver driver = new FirefoxDriver();
driver.get("http://spicejet.com/");
String date ="11/03";
WebElement element = driver.findElement(By.xpath("//input[@id='ctl00_mainContent_view_date1']"));
element.click();
DatePicker(driver,element,date);
public static void DatePicker(WebDriver driver, WebElement element, String date)
{
JavascriptExecutor js = ((JavascriptExecutor) driver);
js.executeScript("arguments[0].setAttribute('value'," + date + ");" , element);
}
In inspect element I get this在检查元素我得到这个
<td class="ui-datepicker-week-end ui-datepicker-current-day"
data-year="2018"
data-month="3"
data-event="click"
data-handler="selectDay">
<a class="ui-state-default ui-state-active" href="#">15</a>
</td>
Try to get all the elements that are inside the "td" tag and place it inside a List .尝试获取“td”标签内的所有元素并将其放入 List 。 Then make a FOR looking for the day you want to select and click on it.然后使用 FOR 查找您要选择的日期并单击它。 Here is the code I use to solve this type of problem.这是我用来解决此类问题的代码。
WebElement dateWidget = driver.findElement("ui-datepicker-div");
List<WebElement> columns = dateWidget.findElements(By.tagName("td"));
for (WebElement cell : columns) {
if (cell.getText().equals(day)) {
cell.findElement(By.linkText(day)).click();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.