我在Selenium的代码下面收到错误

时间:2016-11-08 07:14:31

标签: java selenium xpath indexoutofboundsexception

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();

             }
         }

例外:

  

线程中的异常&#34; main&#34; java.lang.IndexOutOfBoundsException:索引:1,大小:1 at java.util.ArrayList.rangeCheck(Unknown Source)at java.util.ArrayList.get(Unknown Source)at eMpower.AddEmployee2.main(AddEmployee2.java:109)

1 个答案:

答案 0 :(得分:0)

第一个数组索引是“0”,而不是“1”,因此对于大小为1的数组,索引1是IndexOutOfBoundsException。没有任何与Selenium相关的东西,但是真正的java