我正在尝试自动化jQueryUi datepicker,年导航的代码工作正常,月导航面临问题。假设我想要选择的日期是2015年1月20日,我编写的代码将在2015年的1月份进行,但随后它将进入第二个while循环,而不应该这样。
我调试了代码但是我仍然看到第二个while循环应该返回false,它返回true。我不知道为什么会发生这样的事情。请找到我为下面的月份导航编写的代码。
while (mon != (driver.findElement(By.cssSelector("span.ui-datepicker-month")).getText()))
{
// System.out.println(driver.findElement(By.cssSelector("span.ui-datepicker-month")).getText());
if (driver.findElement(By.cssSelector("span.ui-datepicker-month")).getText() != "January")
{
driver.findElement(By.cssSelector("a.ui-datepicker-prev")).click();
System.out.println(driver.findElement(By.cssSelector("span.ui-datepicker-month")).getText());
}
if (driver.findElement(By.cssSelector("span.ui-datepicker-month")).getText().equals("January"))
{
break;
}
}
while (mon !=(driver.findElement(By.cssSelector("span.ui-datepicker-month")).getText()))
{
if (mon != driver.findElement(By.cssSelector("span.ui-datepicker-month")).getText())
{
driver.findElement(By.cssSelector("a.ui-datepicker-next")).click();
System.out.println(driver.findElement(By.cssSelector("span.ui-datepicker-month")).getText());
}
if (mon.equals(driver.findElement(By.cssSelector("span.ui-datepicker-month")).getText()))
{
break;
}
}
答案 0 :(得分:0)
最后在工作了一段时间之后,得到了完美的解决方法: -
private static void PickDate(WebDriver驱动程序,String day,String mon,String year)抛出异常{
while(year!= driver.findElement(By.cssSelector(" span.ui-datepicker-year"))。getText())
{
if(Integer.parseInt(year)< Integer.parseInt(driver.findElement(By.cssSelector(" span.ui-datepicker-year"))。getText()))
{
driver.findElement(By.cssSelector(" a.ui-日期选择器-分组&#34))。单击();
的System.out.println(driver.findElement(By.cssSelector(" span.ui-日期选择器月"。))的getText());
}
else if(Integer.parseInt(year)> Integer.parseInt(driver.findElement(By.cssSelector(" span.ui-datepicker-year"))。getText()))
{
driver.findElement(By.cssSelector(" a.ui-日期选择器-NEXT&#34))。单击();
}
else if(Integer.parseInt(year)== Integer.parseInt(driver.findElement(By.cssSelector(" span.ui-datepicker-year"))。getText())){
打破;
}
}
while(!mon.equals(driver.findElement(By.cssSelector(" span.ui-datepicker-month"))。getText()))
{
// System.out.println(driver.findElement(By.cssSelector(" span.ui-datepicker-month"))。getText());
if (!"January".equals(driver.findElement(By.cssSelector("span.ui-datepicker-month")).getText()))
{
driver.findElement(By.cssSelector("a.ui-datepicker-prev")).click();
System.out.println(driver.findElement(By.cssSelector("span.ui-datepicker-month")).getText());
}
if (driver.findElement(By.cssSelector("span.ui-datepicker-month")).getText().equals("January"))
{
break;
}
}
while (!mon.equals(driver.findElement(By.cssSelector("span.ui-datepicker-month")).getText())){
if (!mon.equals(driver.findElement(By.cssSelector("span.ui-datepicker-month")).getText()))
{
driver.findElement(By.cssSelector("a.ui-datepicker-next")).click();
System.out.println(driver.findElement(By.cssSelector("span.ui-datepicker-month")).getText());
}
if (mon.equals(driver.findElement(By.cssSelector("span.ui-datepicker-month")).getText()))
{
break;
}
}
// WebElement dates = driver.findElement(By.cssSelector(" input.hasDatepicker"));
List columns = driver.findElements(By.cssSelector(" table.ui-datepicker-calendar> tbody> tr> td> a"));
//comparing the text of cell with today's date and clicking it.
for (WebElement cell : columns)
{
if ((cell.getText()).equals(day))
{
cell.click();
break;
}
}
}