How can we automate pagination using Selenium Webdriver? Need to click on Next and Previous buttons to iterate through the table.

List<webElement> pagination =driver.findElemnts(By.xpath("//div[@class='nav-pages']//a")); 
// checkif pagination link exists 

if(pagination .size()>0){ 
sop("pagination exists"); 

// click on pagination link 

for(int i=0; i<pagination .size(); i++){ 
pagination.get(i).click(); 

} else { 
sop("pagination not exists");