I have a Search Screen where after I enter the search criteria and search, a Load More button will display if there are more than 30 records. The problem here is it clicks the Load More button for the first time but is not clicking a second or third time.

The scenario is I need to click until the Load More button becomes disabled.

Below is my code any inputs

   List<WebElement> rows = driver.findElements(By.xpath("//tbody[@role='rowgroup']/tr"));
            int rowcount = rows.size();

            String loadmoreXpath = AppXPathsConstants.buttonXpath_replace.replace("XXXX","Load More");
            if(driver.findElements(By.xpath(loadmoreXpath)).size()!= 0) 
            WebElement loadmoreElem = DriverUtil.getFluentWaitElementByXPath(loadmoreXpath,20, driver);
                WebDriverWait wait = new WebDriverWait(driver,60);
                wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[contains(@class,'mat-button-wrapper') and contains(normalize-space(),'Load More')]")));
                WebDriverWait w = new WebDriverWait(driver,10);

            while(driver.findElements(By.xpath("//span[contains(@class,'mat-button-wrapper') and contains(normalize-space(),'Load More')]")).size()>0);

And on running the code i get the below issue:
org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to no longer be visible: By.xpath: //span[contains(@class,'mat-button-wrapper') and contains(normalize-space(),'Load More')] (tried for 10 second(s) with 500 milliseconds interval)
  • Are you receiving any errors when you try to click the button the second time?
    – Kate Paulk
    Sep 24, 2021 at 11:55
  • it is not clicking on the Load more button the second time and there are no errors it just clicks the first time - @Kate Paulk - any inputs
    – jay m
    Sep 24, 2021 at 11:58

2 Answers 2


With what you are attempting to do, you should be using a loop structure.

I'd recommend something like this (no guarantees on the code - I haven't worked with Java in over 20 years):

WebElement loadmore = getFluentWaitElementByXPath(loadmoreXpath, 20, driver);

// assuming "loadmore" is the button to click
// also we need to wait until the extra results are loaded

while (loadmore.isDisplayed() && loadmore.isClickable())
   // do the wait again
   // once the wait is done, search for the button again - this ensures you won't have a stale element exception to worry about
   loadmore = driver.getElement(by.xpath(loadmoreXPath));

You could also use a loop that checks while (!loadmore.isDisabled()). The key thing is that you have a means of checking that the extra set of search results is loaded and relocating the load more button.

You may also need to consider scrolling to the button, since the list may get lengthy.

  • i get what you are saying but just wanted to know if there are more than 10 load more button i need to keep giving this line // once the wait is done, search for the button again - this ensures you won't have a stale element exception to worry about loadmore = driver.getElement(by.xpath(loadmoreXPath)); -@Kate Paulk
    – jay m
    Sep 24, 2021 at 12:32
  • have made changes like this - do { PageUtil.scrolltoBottom(driver); WebDriverWait wait = new WebDriverWait(driver,120); wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[contains(@class,'mat-button-wrapper') and contains(normalize-space(),'Load More')]"))); loadmoreElems.click(); DriverUtil.waitTime(5000); if(!loadmoreElem.isDisplayed()) { break; } } while(loadmoreElem.isDisplayed());
    – jay m
    Sep 24, 2021 at 12:33
  • but leads to stale elemnt exception in the line if(!loadmoreElem.isDisplayed())
    – jay m
    Sep 24, 2021 at 12:33
  • @jaym There is no loop in your logic , so the execution happens only once . You should use some form of loop . But make sure you rediscover the element as the DOM changes after click and this results in stale element error.
    – PDHide
    Sep 24, 2021 at 13:06
  • updated my code with changes - it does lead to stale element exception and have added the wait.until(ExpectedConditions.stalenessOf(loadmoreElems)); but now getting : org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element ([[ChromeDriver: chrome on WINDOWS (fb2814833df250e07c6cf007f92d758e)] -> xpath: //span[contains(@class,'mat-button-wrapper') and contains(normalize-space(),'Load More')]]) to become stale (tried for 10 second(s) with 500 milliseconds interval) -@PDHide
    – jay m
    Sep 24, 2021 at 13:11

I think its because, you are defining loadmoreElems (within the loop) after you perform the click operation. So it click on the Load More button one time because you have defined loadmoreElems outside the loop. After the 1st click you are not defining it before click operation and the previously defined loadmoreElems is no longer valid.

Define it before that and try:

WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//span[contains(@class,'mat-button-wrapper') and contains(normalize-space(),'Load More')]")));
loadmoreElems = DriverUtil.getFluentWaitElementByXPath(loadmoreXpath,20, driver);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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