Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python login into a website with javascript form

I'm attempting to log into my school's website using Requests, but it doesn't get past the log in page and doesn't return the stuff in the password protected pages. All it does is return the HTML of the login page. Twill would not work as this page requires javascript. . The HTML login stuff is

<!--box content-->

<div id="noscript" class="feedback-alert"> To sign in to PowerSchool, you must use a browser that supports and has JavaScript enabled. </div>
<fieldset id="login-inputs" class="hide">

    <div>
          <label>Username</label>
          <input type="text" id="fieldAccount" name="account" value="" size="39"  />
    </div>
    <div>
          <label>Password</label>
          <input type="password" name="pw" value="" size="39"  /><div id="login-help"><a href="/public/account_recovery_begin.html">Having trouble signing in?</a></div>
    </div>
    <div id="translatorInput">
          <label>Translator Sign In</label>
          <input type="password" name="translatorpw" value="" size="39"  />
    </div>
    <div class="button-row">
    <button type="submit" id="btn-enter" title="Sign In To PowerSchool Parent Access" value="Enter" border="0" >Sign In</button>
    </div>
</fieldset> 

  <!-- box content-->

I've checked this answer

My current code is

import requests
payload = {
    'account': 'username',
    'pw': 'password'
}
with requests.Session() as s:
    p = s.post('https://powerschool.-/public/home.html', data=payload)   
    print p.text

    r = s.get('https://powerschool.-/guardian/studentsched.html')
    print r.text

but I can't seem to log into the page. My question is am I suppose to have a payload to press the 'submit' button or something? I've tried 'action' : 'login' and stuff like that but none of it works. Also, I don't need a translatorpw so should I write 'translatorpw': '' or just ignore that? Obviously I put my real username/password in the program on my laptop. Thanks in advance!

Edit: I just used Selenium and it worked very easily.

like image 793
bolkols Avatar asked Nov 20 '15 02:11

bolkols


People also ask

Can I connect Python with JavaScript?

JS2PY works by translating JavaScript directly into Python. It indicates that you may run JS directly from Python code without installing large external engines like V8. To use the module it first has to be installed into the system, since it is not built-in. To use the module it has to be imported.


1 Answers

Try setting up custom headers as some sites reject default requests agent

    import requests

     payload = {
    'account': 'username',
    'pw': 'password'
     }

     headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0'}     

with requests.Session() as s:
    p = s.post('https://powerschool.-/public/home.html', data=payload, headers=headers)   
    print p.text

    r = s.get('https://powerschool.-/guardian/studentsched.html', headers=headers)
    print r.text

Check if there are any additional parameters that are being send with your post requests, if so send it in payload too.

like image 98
rikoudosenin Avatar answered Sep 29 '22 06:09

rikoudosenin