0

I've tried the following:

How to use Python to login to a webpage and retrieve cookies for later usage?

However this requires

resp = opener.open('http://www.example.com/hiddenpage.php')

I can't know the hiddenpage link since it changes with every login and every refresh.

The page is a simple HTTP one and I used WireShark to know the input fields and they are as follows

HTML Form URL Encoded: application/x-www-form-urlencoded
Form item: "mode" = "451"
Form item: "json" = "{"username":"test1,"password":"dragon","testtest":"1","browser":"Chrome_57"}"
Form item: "__RequestType" = "ajax"
Form item: "t" = "Sat Apr 22 2017 15:05:41 GMT 0530 (India Standard Time)"

Do I have to include all the form items in the script or are they provided by default by the request library? As a user visiting the website I only provide username and password, though rest of the fields are editable as well.

This is how login page looks if it helps

Community
  • 1
  • 1
Siddhant
  • 143
  • 1
  • 13

1 Answers1

0

You can use request.session. You can login using a POST request. After that, session will automatically follow the login link as if you're logging in from a browser.

from request import session

Session = session()

payload = {
    "username": test1,
    "password": "dragon",
    "testtest": "1",
    "browser": "Chrome_57"
    }

result = Session.post("http://example.com/",data=payload).content.decode() #Session.get for get request

print(result) #page after logging in.
Dashadower
  • 632
  • 1
  • 6
  • 20
  • Thanks but I still can't seem to login. I can't even know if there is an error message like 'login failed' since this message is not even visible in 'view-source' (chrome). Although if I inspect the element, then it is visible in the HTML. I don't know how is this possible but yeah it is what it is. – Siddhant Apr 22 '17 at 15:09
  • Hmmm... what the the `result` print? – Dashadower Apr 22 '17 at 15:10
  • @Siddhant Try double-checking the login form location and method. Make sure all the parameters are correct. I can't think of another reason why it wouldn't login. – Dashadower Apr 22 '17 at 17:31
  • I did. It's a .jsp if that makes a difference. I've made changes and now my post request (as seen in wireshark) from the script and from the web browser looks exactly same but the subsequent GET response is different. From web browser I being taken to another .jsp page whose link changes with each refresh however from the script I am being taken back to login page. – Siddhant Apr 22 '17 at 21:17
  • Oh. It looks like the request type is an `ajax`. So it doesn't redirect you. You're going to have to analyze the javascript where it handles the login request, and figure out what happens after the request. – Dashadower Apr 23 '17 at 04:54
  • Finally something with which I can move forward in. Thanks!. Do you know of any reference links which can help me? – Siddhant Apr 23 '17 at 09:55
  • Try searching on how ajax works, and I'm pretty sure the webpage is going to use `jquery`, so look up about that, too. – Dashadower Apr 23 '17 at 10:30