Search code examples
luascrapyscrapy-splash

Lua/Splash: Link to next page is not being executed


I am trying to write a Lua script to go to the next page of the website: http://quotes.toscrape.com/js/page/1/

My script:


    function main(splash, args)
      assert(splash:go(args.url))
      assert(splash:wait(0.5))
      assert(splash:runjs('document.querySelector(".next a[href]").click()'))
      splash:set_viewport_full()
      return {
        html = splash:html(),
        png = splash:png(),
        har = splash:har(),
      }
    end

I tried multiple CSS Selectors which all work in the browser. However the Lua script only returns the first page and doesn't open the next.

Does anyone have an idea what could be the cause?

Thank you and best regards Tobi


Solution

  • Problem solved! I just added wait statement for 1 second. Here the correct code:

    function main(splash, args)
      assert(splash:go(args.url))
      assert(splash:wait(0.5))
      assert(splash:runjs('document.querySelector(".next a[href]").click()'))
      assert(splash:wait(1))
      splash:set_viewport_full()
      return {
        html = splash:html(),
        png = splash:png(),
        har = splash:har(),
      }
    end