我们公司开始实施自动化测试,我更喜欢Selenium Webdriver和Java。我也是硒的新手,而且堆栈溢出, 题?如何从Ajax日期选择器中选择日期?我在Java中使用Selenium WebDriver
HTML:
<div class="col-md-3">
<div class="form-group">
<label for="">Open Date <span class="text-danger">*</span></label><input type="text" id="qstarton" required="" name="qstarton" class="form-control datepicker32">
<span class="text-danger" id="error_qstarton">Please fill it.</span>
</div>
</div>
<div class="datepicker datepicker-dropdown dropdown-menu datepicker-orient-left datepicker-orient-top" style="display: block; top: 210px; left: 761.5px;">
<div class="datepicker-days" style="display: block;"><table class="table table-condensed"><thead><tr>
<th colspan="5" class="datepicker-switch">June 2018</th><th class="next" style="visibility: visible;">»</th></tr>
<tr><th class="dow">Su</th><th class="dow">Mo</th><th class="dow">Tu</th><th class="dow">We</th><th class="dow">Th</th>
<th class="dow">Fr</th>
<th class="dow">Sa</th></tr></thead>
<tbody><tr><td class="old disabled day">27</td>
<td class="old disabled day">28</td>
<td class="old disabled day">29</td>
<td class="old disabled day">30</td>
<td class="old disabled day">31</td>
<td class="disabled day">1</td>
<td class="disabled day">2</td> </tr>
同一行会更多,这些与文本框有关的所有事情enter image description here
答案 0 :(得分:0)
请找到执行此操作的代码:
日历:http://jqueryui.com/resources/demos/datepicker/other-months.html
package com.demo.core;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class JqueryCalender {
private static WebDriver driver;
private static final String INNERTEXT = "innerText";
public static void main(String[] args) {
String onwardDate = "19 February 2041";
driver = getChromeDriver(); // get your driver here
driver.manage().window().maximize();
driver.navigate().to("http://jqueryui.com/resources/demos/datepicker/other-months.html");
// setting onward date
setOnwardDate(driver, onwardDate);
}
/** It will set the onward date : format of date should be "1 Mar 2018"
* @param driver
* @param onwardDate
*/
public static void setOnwardDate(WebDriver driver, String onwardDate) {
System.out.println("########## Setting onward date : " + onwardDate);
actionClick(driver, driver.findElements(By.cssSelector("input[id='datepicker']")).get(0)); // clicking on onward calendar
setMonthAndYear(driver, onwardDate); // setting month and year
String exactOnwardDate = onwardDate.split(" ")[0];
WebElement dateElement = driver.findElements(By.cssSelector("a[class='ui-state-default']")).stream().filter(element -> element.getAttribute(INNERTEXT).trim().equals(exactOnwardDate)).findFirst().get();
jsClick(driver, dateElement); // setting the date
}
/** It will set calendar date.
* @param driver
* @param date : date format should be "1 march 2018"
*/
private static void setMonthAndYear(WebDriver driver, String date) {
String completeDate[] = date.split(" ");
String monthYear = completeDate[1] + " " + completeDate[2];
while(!(driver.findElements(By.cssSelector("div.ui-datepicker-title span")).get(0).getAttribute(INNERTEXT).trim() + " " + driver.findElements(By.cssSelector("div.ui-datepicker-title span")).get(1).getAttribute(INNERTEXT).trim()).equals(monthYear)) {
driver.findElements(By.cssSelector("a[title='Next']")).get(0).click();
}
}
/** It will click on a WebElement using javascript
* @param driver
* @param element
*/
public static void jsClick(WebDriver driver, WebElement element) {
System.out.println("######### Performing JS Click on element :" + element);
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
}
/** It will click on an element using Actions class
* @param driver
* @param element
*/
public static void actionClick(WebDriver driver, WebElement element) {
System.out.println("######### Performing Action Click on element :" + element);
Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perform();
}
/**
* @return ChromeDriver instance
*/
private static WebDriver getChromeDriver() {
System.out.println("######### Getting chromedriver ###################");
System.setProperty("webdriver.chrome.driver",
"D:\\ECLIPSE-WORKSPACE\\Selenium-Demo\\src\\main\\resources\\drivers\\chromedriver-2.35.exe");
return new ChromeDriver();
}
}
希望对您有帮助。
您可以复制逻辑并根据日历进行调整。