I am using ScrapyJS and Splash to simulate a form submit button click
def start_requests(self):
        script = """
        function main(splash)
            assert(splash:autoload("https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"))
            assert(splash:go(splash.args.url))
            local js = [[
                var $j = jQuery.noConflict();
                $j('#USER').val('frankcastle');
                $j('#password').val('punisher');
                $j('.button-oblong-orange.button-orange a').click();
            ]]
            assert(splash:runjs(js))
            local resumeJs = [[
                function main(splash) {
                    var $j = jQuery.noConflict();
                    $j(document).ready(function(){
                        splash.resume();
                    })
                }
            ]]
        assert(splash:wait_for_resume(resumeJs))
            return {
                html = splash:html()
            }
        end
        """
        splash_meta = {'splash': {'endpoint': 'execute', 'args': {'wait': 0.5, 'lua_source': script}}}
        for url in self.start_urls:
            yield scrapy.Request(url, self.after_login, meta=splash_meta)
def after_login(self, response):
        print response.body
        return
After doing splash:runjs(js), I am resorting to  tried splash:wait(5)splash:wait_for_resume to get the result. This might not always work ( network latency ), so is there a better way?
It turns out the only way is to use splash:wait() but do it in a loop and check for availability of some element (like footer).
def start_requests(self):
        script = """
        function main(splash)
            assert(splash:autoload("https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"))
            assert(splash:go(splash.args.url))
            local js = [[
                var $j = jQuery.noConflict();
                $j('#USER').val('frankcastle');
                $j('#password').val('punisher');
                $j('.button-oblong-orange.button-orange a').click();
                $j('body').empty() // clear body, otherwise the wait_for footer will always be true
            ]]
            assert(splash:runjs(js))
            function wait_for(splash, condition)
                while not condition() do
                    splash:wait(0.05)
                end
            end
            wait_for(splash, function()
                return splash:evaljs("document.querySelector('#footer') != null")
            end)
            return {
                html = splash:html()
            }
        end
        """
        splash_meta = {'splash': {'endpoint': 'execute', 'args': {'wait': 0.5, 'lua_source': script}}}
        for url in self.start_urls:
            yield scrapy.Request(url, self.after_login, meta=splash_meta)
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With