简体   繁体   中英

Month dropdown not working properly in datepicker in selenium webdriver

I am trying to automate the accounts creation page of Salesforce in selenium webdriver java through a demo account. I am reading the data from the excel file in D drive.

In the datepicker field, the year dropdown works fine but the month dropdown select is not working correctly. The month dropdown opens, and the desired month is also highlighted and it also reaches and highlights the desired date tag in the page but then it jumps back to current month ie November and selects the date from there.

I am not able to understand this behavior.

String date, mon, year;
    int month, day;
    date = sh.getCell(27, 3).getContents();
    year = date.substring(6, 10);
    month = Integer.parseInt(date.substring(0, 2));
    day = Integer.parseInt(date.substring(3, 5));
    month = month-1;
    switch(month)
    {
        case 0 : mon = "January";   break;
        case 1 : mon = "February";  break;
        case 2 : mon = "March";     break;
        case 3 : mon = "April";     break;
        case 4 : mon = "May";       break;
        case 5 : mon = "June";      break;
        case 6 : mon = "July";      break;
        case 7 : mon = "August";    break;
        case 8 : mon = "September";     break;
        case 9 : mon = "October";       break;
        case 10 : mon = "November";     break;
        case 11 : mon = "December"; break;
        default : mon="Incorrect month input";
    }
    System.out.println(mon);

    Select dty;
    driver.findElement(By.xpath(".//*[@id='00N9000000Ctc7X']")).click();
    dty = new Select(driver.findElement(By.xpath(".//*[@id='calYearPicker']")));
    dty.selectByVisibleText(year);

    Select dtm;
    driver.findElement(By.xpath(".//*[@id='calMonthPicker']")).click();
    dtm = new Select(driver.findElement(By.xpath(".//*[@id='calMonthPicker']")));
    dtm.selectByVisibleText(mon);
    Thread.sleep(5000);

    WebElement dp = driver.findElement(By.xpath(".//*[@id='datePicker']/div[2]"));
    List<WebElement> colmn;
    colmn = dp.findElements(By.tagName("td"));
    int dat=0; flag=0;

    for(WebElement we : colmn)
    {
        System.out.println(we.getText());
        dat = Integer.parseInt(we.getText());
        String clas = we.getAttribute("class");
        try 
        {                   
            if((day==dat) && ((clas.equals("weekday") || (clas.equals("weekend")))))
            {
                we.click(); Thread.sleep(5000);
                flag=1;
                break;
            }
        }
        catch(Exception e)
            {
                e.printStackTrace();
            }

    }   
    if(flag==0)
        System.out.println("Incorrect Date");

Given Below is the comment given by Subh which answers my question correctly.

True. You would've to create object of Select class, like this Select sel = new Select(driver.findElement(By.xpath("//xpath of the element"))); and then use 'selectByValue or selectByVisibleText or selectByIndex' method(s) to select the proper dropdown value. On the other hand, sometimes the dropdown is not made by using "select" tag, therein you would've to click on the dropdown first, and then click on the resultant option in the dropdown.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM