Ramprasad Patil Ramprasad Patil - 23 days ago 16
Java Question

I'm getting the error below code in Selenium

public class AddEmployee2 {

public static void main(String[] args) throws Exception {
String dateTime ="26/02/1989";

WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://testing-1/Premium_Next_03June2016/default.aspx");
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

driver.findElement(By.id("txtUsername")).sendKeys("Super");
driver.findElement(By.id("txtPassword")).sendKeys("sa");
driver.findElement(By.id("btnLogin")).click();

Actions act = new Actions(driver);
/* Find the xpath of File menu*/
WebElement element = (driver.findElement(By.xpath("id('maintext-1')")));
act.moveToElement(element).perform();
driver.findElement(By.xpath(".//*[@id='subtext-1-10']/span")).click();
driver.findElement(By.id("_ctl0_MainBody_cmdNew")).click();
driver.findElement(By.id("_ctl0_MainBody_txtCode")).sendKeys("501");
driver.findElement(By.id("_ctl0_MainBody_txtName")).sendKeys("Rahul");
//button to open calendar

WebElement selectDate = driver.findElement(By.xpath(".//*[@id='_ctl0_MainBody_txtDOB_TextDatepicker']"));
selectedDate.click();
WebElement nextLink = driver.findElement(By.xpath("//div[@id='_ctl0_MainBody_txtDOB_TextDatepicker_CalendarExtender_nextArrow']"));
WebElement midLink = driver.findElement(By.xpath("//div[@id='_ctl0_MainBody_txtDOB_TextDatepicker_CalendarExtender_title']"));
WebElement previousLink = driver.findElement(By.xpath(" //div[@id='_ctl0_MainBody_txtDOB_TextDatepicker_CalendarExtender_prevArrow']"));
String date_dd_MM_yyyy[] = (dateTime.split(" ")[0]).split("/");
int yearDiff = Integer.parseInt(date_dd_MM_yyyy[2])- Calendar.getInstance().get(Calendar.YEAR);
System.out.println("Test"+Integer.parseInt(date_dd_MM_yyyy[2]));
System.out.println("what is this"+Calendar.getInstance().get(Calendar.YEAR));
System.out.println("Year difference Ram"+yearDiff);

midLink.click();
if(yearDiff!=0){

//if you have to move next year

if(yearDiff>0){

for(int i=0;i< yearDiff;i++){

System.out.println("Year Diff next->"+i);

nextLink.click();

}

}

//if you have to move previous year

else if(yearDiff<0){

for(int i=0;i< (yearDiff*(-1));i++){

System.out.println("Year Diff previous->"+i);
previousLink.click();

}
Thread.sleep(1000);

//Get all months from calendar to select correct one


List<WebElement> list_AllMonthToBook = driver.findElements(By.xpath("//div[@id='_ctl0_MainBody_txtDOB_TextDatepicker_CalendarExtender_months']//table[@id='_ctl0_MainBody_txtDOB_TextDatepicker_CalendarExtender_monthsTable']//tbody[@id='_ctl0_MainBody_txtDOB_TextDatepicker_CalendarExtender_monthsBody']"));
list_AllMonthToBook.get(Integer.parseInt(date_dd_MM_yyyy[1])-1).click();

// System.out.println("Get all Months"+(Integer.parseInt(date_dd_MM_yyyy[1])-1));

Thread.sleep(1000);

//get all dates from calendar to select correct one

List<WebElement> list_AllDateToBook = driver.findElements(By.xpath("//div[@id='datetimepicker_dateview']//table//tbody//td[not(contains(@class,'k-other-month'))]"));

list_AllDateToBook.get(Integer.parseInt(date_dd_MM_yyyy[0])-1).click();

}
}


Exception:


Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 at java.util.ArrayList.rangeCheck(Unknown Source) at java.util.ArrayList.get(Unknown Source) at eMpower.AddEmployee2.main(AddEmployee2.java:109)

Answer

1st index of arrays is "0", not "1", So with array with size 1, index 1 is IndexOutOfBoundsException. Nothing related to Selenium, but true java

Comments