【发布时间】:2016-02-22 03:43:00
【问题描述】:
我想单击 makemytip.com 中的特定日期。我写了下面的代码。一切正常。除了我无法选择日期。我可以导航到我想要的月份,但无法选择日期。提前感谢您的帮助。
当我运行以下程序时,我收到以下错误
线程“main”org.openqa.selenium.ElementNotVisibleException 中的异常:元素当前不可见,因此可能无法与之交互
package com;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Calendar_MMT {
static WebDriver d;
public static void main(String[] args) throws ParseException {
d = new FirefoxDriver();
d.manage().window().maximize();
d.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
d.get("http://makemytrip.com");
d.findElement(By.xpath("//*[@id='start_date_sec']/span[1]/span[1]")).click();
selectDate("02/03/2016");
}
public static void selectDate(String date) throws ParseException{
SimpleDateFormat df=new SimpleDateFormat("dd/MM/yyyy");
Date dateToBeSelected = df.parse(date);
Date currentDate = new Date();
String monthYearDisplayed = d.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/div")).getText();
System.out.println(monthYearDisplayed);
String month = new SimpleDateFormat("MMMM").format(dateToBeSelected);
String year = new SimpleDateFormat("yyyy").format(dateToBeSelected);
String day = new SimpleDateFormat("d").format(dateToBeSelected);
String monthYearToBeSelected = month+" "+year;
System.out.println(monthYearToBeSelected);
while(true){
if(monthYearToBeSelected.equals(monthYearDisplayed)){
// I FEEL BELOW LINE HAS PROBLEM
d.findElement(By.xpath("//a[text()='"+day+"']")).click();
System.out.println("Found and Selected");
break;
}else{ //navigate to correct month and year
if(dateToBeSelected.after(currentDate)){
d.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[2]/div/a/span")).click();
}else{
d.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/a/span")).click();
}
}
monthYearDisplayed = d.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/div")).getText();
}
}
}
【问题讨论】:
标签: java selenium selenium-webdriver