Albertay Albertay - 12 days ago 5
Javascript Question

How to log in to Instagram with Javascript?

Mission



I am trying to fill in the username and password fields and then click the log in button to log in to Instagram using Javascript.

What I've tried (with correct login credentials):



Instagram Login Page

document.querySelector('[name=username]').value = "USERNAME";
document.querySelector('[name=password]').value = "PASSWORD";

var buttons = document.getElementsByTagName('button');
for (var i = 0; i < buttons.length; i++) {
if (buttons[i].innerText == 'Log in') {
buttons[i].click();
break;
}
}


Problem



Even though the username and password fields contain a correct username and password, I get this error:


The username you entered doesn't belong to an account. Please check your username and try again.


Question



Is it possible to log in to Instagram using Javascript? If so, how can it be done?

Answer

The root problem is that Instagram is built using React, and in React, the UI is built out of "component" objects which update the DOM with their state whenever appropriate. By changing the text field's value via the DOM, the components' states are not being updated, so Instagram's React code doesn't know about the new value. When you click the button, the original value (which is blank) gets sent in the login request.

If you want to pull this off you'll have to find a way to trick React into updating the component state. If I were you I'd play around with triggering a synthetic event on one of the elements to make React think the user typed something. I'm not a React expert though, so I can't help much beyond that.

Comments