Troubleshooting PhantomJS Instagram Login Code

I wrote this PhantomJS script to automate Instagram login. He can successfully fill out the form fields and click the submit button, but he is always redirected back to the login screen with the following message:

Invalid username or password.

I am 100% sure that the credentials are correct, and I tried this with several Instagram accounts.

I am running a script on a Mac from a terminal:

$ phantomjs --ssl-protocol=any --cookies-file=cookies.txt script.js username password 

Note the --ssl-protocol=any option. Without it, PhantomJS cannot even open the login page due to HTTPS. See this question .

Here is the code. I based this tutorial and updated the code to account for the latest changes in the source code of the Instagram login page.

 var system = require('system'); var username = system.args[1]; var password = system.args[2]; var page = require('webpage').create(); page.open('https://instagram.com/accounts/login/', function (status) { if (status === "success") { page.evaluate(function (uid, pwd) { var username_field = document.getElementById('lfFieldInputUsername'); username_field.value = uid; var password_field = document.getElementById('lfFieldInputPassword'); password_field.value = pwd; }, username, password); var point = page.evaluate(function () { var element = document.getElementsByTagName('button')[0]; var rect = element.getBoundingClientRect(); return { x: rect.left + Math.floor(rect.width / 2), y: rect.top + Math.floor(rect.height / 2) }; }); page.render('before-submit.png'); page.sendEvent('click', point.x, point.y); } setTimeout(function () { var error = page.evaluate(function () { var element = document.getElementById('errorAlert'); var error_message = false; if (element !== null) { error_message = element.innerText.trim(); } return error_message; }); page.render('after-submit.png'); if (!error) { console.log('Login Successful: ' + page.url); } else { console.log('Login Failed: ' + error); } phantom.exit(0); }, 5000); } ); 

And here is what I have tried so far:

  • Setting cookies. Besides adding the command line --cookies-file=cookies.txt , I also tried phantom.addCookie in code. It doesn't seem to make any difference.
  • Explicitly sending auth headers. See this question .
  • Change the time zone. Not sure if that makes sense. See this question .

PhantomJS seems to have some SSL issues. Should I just refuse to use this for this purpose?

+5
source share
1 answer

I think the problem with the Instagram name is ReactJs. When you set the value of a text field, React does not change state. This means that when you submit the form, the empty password and password are sent empty.

I have not tried, but I think that you should fill the text inputs with a keystroke event, because the internal state (React) changes only when the input text changes. A parameter value in your path does not raise a change event.

0
source

Source: https://habr.com/ru/post/1232328/


All Articles