'webdriver io js parsing

I need to copy links from (h5 a) elements on all pages using pagination. My code collects links only from the first page, it goes through the rest of the pages but does not collect links. What is the problem here? Thanks!

const linksCollector = async () => {

  let browser;
    try {
      browser = await remote({
        capabilities: { browserName: "chrome" },
      });

      await browser.navigateTo(link);

      const links = [];
      const pages = await browser.$$(".pagination__item");
      const paginationBtn = await browser.$(".pagination__next-icon");

      for (let i = 0; i < pages.length - 2; i++) {
        const linksArr = await browser.$$("h5 a");
        for (let i = 0; i < linksArr.length; i++) {
          const link = await linksArr[i].getAttribute("href");
          links.push(await link);
        }

        await paginationBtn.click();
      }
      await writeData(links);

      await browser.deleteSession();
    } catch (err) {
      console.error(err);
      return browser.deleteSession();
    }
  }
};


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source