0

I am making a script that requires a spotify login, and I have this code:

require 'mechanize'

mechanize = Mechanize.new

page = mechanize.get('https://accounts.spotify.com/en/login')

form = page.forms.first

form['username'] = 'username'
form['password'] = 'password'

page = form.submit

this code leaves me with this error:

:in `<top (required)>': undefined method `[]=' for nil:NilClass (NoMethodError)

however, if I try the same code on facebook (of course chaning the names), or any other site I've tried I get no error. Could this have something to do with spotifys form not having a post method? I am not too familiar with this kind of stuff, and help would be appreciated!

henrik
  • 88
  • 9

2 Answers2

1

Check out the body of the page (page.body) and you will see that it's empty.

This is because the body of that page, including form, is rendered by javascript (Angular).

Since Mechanize can't handle javascript you need another solution. Check out: How do I use Mechanize to process JavaScript?

maicher
  • 2,625
  • 2
  • 16
  • 27
  • However, even the ECMAScript-generated DOM doesn't contain a form, and even if it did, the ID of the login and password fields aren't `username` and `password`. Practically, *every single assumption* the script makes is wrong. – Jörg W Mittag Dec 25 '17 at 18:21
  • 1
    I see a form, and I see username and password. I can open up a js console and inspect it with `$('[name=password]')` - the problem is, mechanize can't see it. – pguardiario Dec 28 '17 at 23:31
0

You are trying to call the []= method on an object which does not understand that method, in particular, you are trying to call it on the object nil which is an object that represents the absence of something.

There are two places where you call the []= method, on lines 9 and 10, both times on the same object, namely on the object referenced by the variable form. So, your problem is that form is nil for some reason.

Where does form come from? It is assigned on line 7 to the result of calling the first method on the forms accessor of the page object. Ergo, for some reason, first returns nil.

And it's easy to see why: the page you linked to doesn't have a form, so it clearly also doesn't have a first form.

Ergo, you get the error because you expect there to be a form where there isn't any.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653