Java 如何在Selenium WebDriver中使用日期选择器选择特定日期
我正在开发SeleniumWebDriver并使用Java。我想从下面屏幕中显示的日期选择器中选择日期范围 目前,我可以选择日期选择器和更改月份选择,但无法选择日期值(即-12/10/2018) 日期选择器选择 下面是我编写的Java代码:Java 如何在Selenium WebDriver中使用日期选择器选择特定日期,java,html,selenium,selenium-webdriver,Java,Html,Selenium,Selenium Webdriver,我正在开发SeleniumWebDriver并使用Java。我想从下面屏幕中显示的日期选择器中选择日期范围 目前,我可以选择日期选择器和更改月份选择,但无法选择日期值(即-12/10/2018) 日期选择器选择 下面是我编写的Java代码: driver.findElement(By.linkText("Leave")).click(); driver.findElement(By.linkText("Apply Leave")).click(); Thread.sle
driver.findElement(By.linkText("Leave")).click();
driver.findElement(By.linkText("Apply Leave")).click();
Thread.sleep(2000);
driver.findElement(By.id("LeaveStartDate")).click();
driver.findElement(By.className("next")).click();
driver.findElement(By.className("day")).sendKeys("12");
driver.findElement(By.id("LeaveEndtDate")).click();
driver.findElement(By.className("next")).click();
driver.findElement(By.className("day")).sendKeys("12/11/2018");
}
}
您需要找到显示“开始日期”的框,并将密钥发送到该元素,而不是发送到日期选择器本身
driver.findElement(By.id("LeaveStartDate")).sendKeys("12/10/2018");
然后对结束日期执行同样的操作。要在更改月份后选择日期,您可以创建一个列表并将所有日期存储在列表中。然后使用“.getText()”逐个获取文本,并尝试将结果日期与预期日期进行比较,然后单击它。通过这种方式,您可以使用日期值参数化脚本 感谢大家对我的问题的回复。。。我尝试过Xpath及其对我有用的功能 详情如下: 使用下面的代码,我一直在“日期选择器”中选择开始和结束日期
结论:Xpath是那些无法执行“Jquery日期选择器”的selenium Java代码的人的解决方案。您可以尝试此实用程序
public void selectDate(String date){ //Here any date you can give
WebElement eval=driver.findElement(By.xpath("//div[contains(@class,'datepicker-days')]/table/tbody"));
List<WebElement> alldates = eval.findElements(By.tagName("td"));
for(WebElement cell:alldates){
String day=cell.getText();
if (cell.getText().contains(date)) {
driver.findElement(By.xpath("//div[contains(@class,'datepicker-days')]/table/tbody/tr/td[text()='"+day+"']")).click();
break;
}
}
}
public void selectDate(String date){//这里是您可以给出的任何日期
WebElement eval=driver.findElement(By.xpath(“//div[contains(@class,'datepicker-days'))]/table/tbody”);
列出所有日期=eval.findElements(按.tagName(“td”));
for(WebElement单元格:alldates){
String day=cell.getText();
如果(cell.getText().contains(date)){
findElement(By.xpath(“//div[contains(@class,'datepicker-days'))]/table/tbody/tr/td[text()='“+day+”])。单击();
打破
}
}
}
注意:如果你想从另一个月选择日期,只需导航到该月,之后你就可以使用此实用程序。你能提供HTML示例吗,从图像中很难理解。你能检查显示“开始日期”的框并将HTML复制到这里吗?谢谢各位……帮助并回答了我的问题。。。它将与XPath一起工作…它不会对我起作用,但是我已经尝试过XPath,它对我起作用…感谢您的帮助无需担心。我假设“开始日期”字段具有我使用的ID:)如果我的答案对您有帮助,您会接受吗?;)
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;
public class dateTimePicker {
WebDriver driver;
@Test
public void dateTimePicker() {
System.setProperty("webdriver.chrome.driver",
"C://Chrome driver exe path");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://jqueryui.com/resources/demos/datepicker/other-months.html");
// click to open the date time picker calendar.
WebElement dateBox = driver.findElement(By.id("datepicker"));
// click to open the date time picker calendar.
dateBox.click();
// Fill date as mm/dd/yyyy as 10/6/2019
dateBox.sendKeys("10/6/2019");
// Press tab to shift focus to time field
dateBox.sendKeys(Keys.TAB);
// close the driver
driver.close();
}
}
public void selectDate(String date){ //Here any date you can give
WebElement eval=driver.findElement(By.xpath("//div[contains(@class,'datepicker-days')]/table/tbody"));
List<WebElement> alldates = eval.findElements(By.tagName("td"));
for(WebElement cell:alldates){
String day=cell.getText();
if (cell.getText().contains(date)) {
driver.findElement(By.xpath("//div[contains(@class,'datepicker-days')]/table/tbody/tr/td[text()='"+day+"']")).click();
break;
}
}
}