0

I'm trying to log in to the webpage(http://quotes.toscrape.com/login) using splash and lua as scripting. However, I'm not able to login into the website.

script = """

        function find_search_input(inputs)
            if #inputs == 1 then
                return inputs[1]
            else
                return inputs
            end
        end


        function find_input(forms)
            local potential = {}
            for _, form in ipairs(forms) do
                local inputs = form.node:querySelectorAll('input:not([type="hidden"])')
                if #inputs ~= 0 then
                    local input = find_search_input(inputs)
                    if input then
                        return form, input
                    end
                    potential[#potential + 1] = {input=inputs, form=form}
                end
            end
            return potential[1].form, potential[1].input
            end

        function main(splash)
            local url = splash.args.url
            assert(splash:go(url))
            assert(splash:wait(10))

            splash:set_viewport_full()

            local forms = splash:select_all('form')
            local form, input = find_input(forms)
            input['username'] = 'foobar'
            input['password'] = 'foobar'

            assert(splash:wait(0))
            assert(form:submit())

            return {
                html = splash:html()

            }


          end
        """
headers = {
            'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36'
        }
        yield SplashRequest('http://quotes.toscrape.com/login', self.parse, endpoint='execute', args={
            'lua_source': script,
            'wait': 5
        }, splash_headers=headers, headers=headers)

And the form is

<form action="/login" method="post" accept-charset="utf-8">
        <input type="hidden" name="csrf_token" value="BJNFrtYLDnpzITvSyQWOXhqCwKbscUEkGReiVjlZfogxAdumaPHM">
        <div class="row">
            <div class="form-group col-xs-3">
                <label for="username">Username</label>
                <input type="text" class="form-control" id="username" name="username">
            </div>
        </div>
        <div class="row">
            <div class="form-group col-xs-3">
                <label for="username">Password</label>
                <input type="password" class="form-control" id="password" name="password">
            </div>
        </div>
        <input type="submit" value="Login" class="btn btn-primary">

    </form>

My requirement is to get the inputs in a form-based on their input type. Suppose in a sign-in form if we get input type of email or text then we will set our username as value to it. Similarly, if a field type is a password then we will our password as value to it. And if it contains some hidden fields like csrf_tokem then we will fetch its value from the form as pass it along with it.

input['type = email || type = text'] = 'our_user_name'
input['type = password'] = 'our_passwords'
input['type = hidden']= 'value_extracted from the form'

I hope these may be some simple thing. But I couldn't figure to implement in lu as I don't have prior experience in that. Anyway thanks in advance for helping me to solve this.

Aaditya R Krishnan
  • 495
  • 1
  • 10
  • 31

1 Answers1

0

I am a newer to lua, Thanks for your question. I just got the inspiration while reading this question. The fellow script should be helpful, if you are too busy to solve it.

function main(splash)
    splash:set_viewport_size(1366, 768)
    splash:set_user_agent('Splash bot')
    assert(splash:go("http://quotes.toscrape.com/login"))
    assert(splash:wait(0.5))
  
    local form = splash: select('form')
    local values = {
        username = 'foobar',
        password = 'foobar',
    }
    assert(form:fill(values))
    assert(form:submit())
    splash:wait(5)
    return {
        png = splash:png(),
    }
end
Grayrigel
  • 3,474
  • 5
  • 14
  • 32