david hol david hol - 10 months ago 53
Scala Question

Loop over all pages via recursion

I have several pages and i want to loop over all pages via recursion:

So i have this

function to check if i have another page to visit:

def isNextPageExist(): Boolean = {


And get next page function that click on some
and get the next page:

def getNextPage(): Unit = {


Now inside this function i want to visit first page and do my stuff and in case i have another page(s) call the function again and again, and inside each page make some calculation and return

def getAllPages(value: Double):Double = {

// get all elements form page inside `value`
getNextPage // get the next page
getAllPages(value) // call the function again



This is only the
code and i will glad for some help.


David, try to avoid Unit return type if your function is named like getNextPage(). Better use Option[Page] and return either Some(page) or None. But the simplest code could be better like following:

case class Page(url:String)

def calculateValue:Double = {here you calculate your value on the current page}

def processPage(page: Option[Page]):List[Double]{
  page match{
    case Some(p) => {
      val linksToNextPage = driver.findElements(By.xpath("//your xpath goes here")
      if(linksToNextPage.size > 0){
        val nextPageLink = Page(linksToNexPage.head.getText())
        calculateValue() :: processPage(Some(Page(nextPageLink))
      } else{
        calculateValue() :: processPage(None)
    case None => Nil

Then you call it with the first web page as processPage(Some(Page("url to first page"))) and in the result you will have a list of values calculated for each page. My code is also a pseudo-code but you should be able to make it work.