0

Am trying to automate a date picker,on clicking the ok button date should be displayed in date box.I have two date pickers like for a start date and the other is for the end date.One date picker is registering the date but the other date picker is not registering the date,neither i get any error.Please help,it's urgent.Here is my code..

package DatePicker;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class DatePickerTest 
{
    WebDriver driver;
    Actions builder;
    @BeforeMethod
    public void setup()
    {
    System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
    driver = new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
    driver.get("http://localhost/able/public/index.php/get_inputs");
    driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
    System.out.println("Hello client");
    }

    @Test
    public void TestDate() throws Exception
    {
     WebDriverWait wait=new WebDriverWait(driver,3);
        Boolean invisible=wait.until(ExpectedConditions.invisibilityOfElementLocated(By.className("preloader")));
        if(invisible)
        {
            driver.findElement(By.xpath("//*[@id=\"add-edit-form\"]/div[3]/div[1]")).click();

        }
        Thread.sleep(2000);




        WebElement web11=driver.findElement(By.className("dtp-btn-ok"));
         web11.click();
         Thread.sleep(2000);


         driver.findElement(By.id("Contract_e_date")).click();
         Thread.sleep(1000);

         builder= new Actions(driver);
         //Thread.sleep(1000);
         WebElement web2=driver.findElement(By.className("dtp-btn-ok"));
         //Thread.sleep(1000);
         builder.moveToElement(web2).click(web2);
            //web2.click();
            Thread.sleep(1000);
         builder.perform();



    }


    }





kreya
  • 1,091
  • 5
  • 24
  • 52

1 Answers1

0

Look here:

WebElement web11=driver.findElement(By.className("dtp-btn-ok"));
WebElement web2=driver.findElement(By.className("dtp-btn-ok"));

If your 2 datepickers have the same CSS class - Selenium will always find the first one and perform operations on the first item found.

Quick and dirty solution would be switching to WebDriver.findElements() function which returns the List of WebElements like:

List<WebElement> datepickers = driver.findElements(By.className("dtp-btn-ok"));
WebElement web11 = datepickers.get(0);
WebElement web2 = datepickers.get(1);

A better solution would be choosing the right Locator Strategy so your locator would uniquely match this or that datepicker (look into their parents, associated text, etc.)

Also be aware that using Thread.sleep is a some form of a performance anti-pattern, consider using WebDriverWait instead, check out How to use Selenium to test web applications using AJAX technology for more details if needed.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Thank you so much for the solution,if i take only two date pickers in a different page and run according to the above given logic it works,but in my project with all other elements with the same code it doesn't work. – kreya Jul 25 '19 at 03:30
  • The solution given before to the datepicker is different from my datepicker issue(for which my question is marked as duplicate).The date picker am using has a ok button.On click of which i just want to register the date.My code is running fine,i get no error,no exception but the date doesn't get displayed on click of the ok button.The solution given to me is useful,But again am facing same problem.Could anyone give me any alternative solution?Many thanks in advance. – kreya Jul 25 '19 at 06:03