I'm trying to get the scroll time of a carousel. I tried to take time when 1st element is selected and time when 2nd element is selected, then deduct 2nd time from 1st time and get the duration.

Below is my code.

    //list of carousel button
    List<WebElement> button = driver.findElements(By.xpath("//ul/li/button"));
    //calendar instance
    Calendar calendar = Calendar.getInstance();
    //start time 
    int scrollStartTime = 0 ;
    //end time
    int scrollEndTime = 0 ;
    for(int btn=0;btn<button.size();btn++) {
        if(button.get(0).isSelected()) {
        //get start time in mill seconds
        long startTime = calendar.getTimeInMillis();
        //convert to int
        scrollStartTime = (int)startTime;
        }if(button.get(1).isSelected()) {
        //get end time
        long endTime = calendar.getTimeInMillis();
        //convert to int
        scrollEndTime = (int)endTime;
    //get the time gap take to change images
    int scrollerTime = scrollEndTime - scrollStartTime ;

I am not getting any out put. There are no errors. Can some one guide me to get the out put?

  • Is the list populated or empty? If empty, you won't see anything. Also, if neither button is "selected", no code will also be executed in the loop. Jun 7 at 7:56
  • @JoãoFarias each button has an image banner. when image is displayed, button is selected Jun 7 at 8:36
  • I'm not sure what is your goal, but your measurement will not be very accurate as there is a lot going on between the call to isSelected() and the results
    – Rsf
    Jun 7 at 13:44

1 Answer 1


According to the code provided, there are a few hypotheses that I might think of.

Since you said that you are not getting any output I would formulate that this code might not even being executed. The reason for my statement relies on the following two pieces of your code:

//start time 
int scrollStartTime = 0 ;

//end time
int scrollEndTime = 0 ;

//get the time gap take to change images
int scrollerTime = scrollEndTime - scrollStartTime ;

This means that even if the list is empty and your for loop is not run you at least would see the number zero (0) provided in your terminal.

Lets assume for now that indeed the zero (0) appears in your terminal but you didn't noticed, so the issue might rely on the for loop. The reason why is that you're are stating that you have a list of 2 elements (considering that indeed your list is not empty) but the time to iterate a 2 element list is almost instant, so there is a high change that your code already finished iterating without any of the items even being selected, this means that none of the if statements is executed. (you might want consider using breakpoints to confirm that)

Since I don't have the full insight of the application you're trying to test it is hard to provide you a concrete answer but you might want to change your logic to wait for when the specific event occurs and capture its time (in this case the specific button is selected). Below I try to provide a working example that possibly may solve your problem and give you an answer to the time that it takes to move the carousel. (in the example instead of a selected element, I wait for an element with known attribute but the principle that you have to apply is very similar)

Please note that the code provided below doesn't follow the correct practice for coding a good automation and it is only to provide a quick example of what you might do, so consider adapting it to a correct practice.

//element selectors
@FindBy(xpath = "//div[@id='carouselExampleCaptions']")
private WebElement carouselElementContainer;

@FindBy(xpath = "//div[@id='carouselExampleCaptions']//div[@class='carousel-inner']/div")
private List<WebElement> carouselElements;


 public void deltaTimeForCarouselElementSelect(){

    //site with carousel that automatically switch images

    WebDriverWait driverWait = new WebDriverWait(driver, Duration.of(15, ChronoUnit.SECONDS));


    driverWait.until(ExpectedConditions.attributeContains(carouselElements.get(0), "class", "carousel-item active"));

    double time1 = getMilliSecTime();
    System.out.printf("Time for first event: %s milliseconds %n", time1);

    driverWait.until(ExpectedConditions.attributeContains(carouselElements.get(carouselElements.size() - 1), "class", "active"));
    double time2 = getMilliSecTime();

    System.out.printf("Time for last event: %s milliseconds %n", time2);

    double DeltaInMilli =  time2 - time1;

    System.out.printf("Time that took between the two events - %f milliseconds %n", DeltaInMilli);

private long getMilliSecTime(){
    Date date = new Date();

    return date.getTime();

Running this method will provide a similar output as the one displayed below:

Time for first event: 1.654638603088E12 milliseconds  
Time for last event: 1.654638613128E12 milliseconds
Time that took between the two events - 10040.000000 milliseconds
  • I am getting expected condition failed for driverWait.until(ExpectedConditions.attributeContains(carouselElements.get(0), "class", "active")); Jun 8 at 3:21
  • @DarshaniKaushalya one of the selectors that I provided was indeed not the best one, hopefully with the new edit of the code you may be able to run it without any further complications. If it is still not working properly for you, since I cannot reproduce the failure, consider go to the URL and try to debug it using dev tools so you see the behaviour changes in the DOM and update the code with a better selector. The most important is for you to understand the code of the example and see if it is something that might provide answers to your questions. Jun 8 at 8:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.