I'm trying to log into our PowerSchool server through Python and Mechanize, but I can't seem to successfully do it. A tricky part of this is that the login form only has a single field for inputting both my username and password, separated by a semicolon. Here is the webpage: https://powerschool.laalliance.org/admin/home.html
Can someone tell me if my code is correct?
br = mechanize.Browser()
br.set_handle_robots(False)
br.set_handle_refresh(False)
br.addheaders = [("User-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:26.0) Gecko/20100101 Firefox/26.0")]
br.open('https://powerschool.laalliance.org/admin/')
br.select_form(name='LoginForm')
br.set_all_readonly(False)
#br.find_control('pstoken').readonly = False
#br.form['pstoken'] = '123123asdfasdf123123'
br.form['password'] = 'jdoe;' + pw
br.method = 'POST'
response = br.submit()
print response.read()
Checked the website, and if you do right-click on the form, then 'inspect element' you will see that the name of the form is : "password"
Hence, you should replace your line:
br.select_form(name='LoginForm')
to
br.select_form(name='password')
Does that solve your problem?
Related
i'm building a python app that is using a login interface to automatically sign in to facebook by passing the users login data directly. Main Goal is to get the access_token from facebook.
the facebook login skript looks like this:
import mechanize
br = mechanize.Browser()
br.set_handle_equiv( True )
br.set_handle_gzip( True )
br.set_handle_redirect( True )
br.set_handle_referer( True )
br.set_handle_robots( False )
br.set_handle_refresh( mechanize._http.HTTPRefreshProcessor(), max_time = 1)
br.addheaders = [ ( 'User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1' ) ]
user = 'me#mymail.com'
password = 'myPass'
# note that client_id is pseudo-nr
url = 'https://m.facebook.com/dialog/oauth?client_id=123456789012345&redirect_uri=https://www.facebook.com/connect/login_success.html&response_type=token'
#Open URL and submit
br.open(url)
br.select_form(nr=0)
br.form['email'] = user
br.form['pass'] = password
br.submit()
result = br.geturl()
print result
Actually the script is working pretty fine, but there seems to be a redirecting problem after
br.submit()
When logging in to the given URL via Firefox, after manually adding username/password and pressing the submit button, the following URL is called:
https://www.facebook.com/login.php?login_attempt=1&next=https%3A%2F%2Fwww.facebook.com%2Fv2.0%2Fdialog%2Foauth%3Fredirect_uri%3Dhttps%253A%252F%252Fwww.facebook.com%252Fconnect%252Flogin_success.html%26response_type%3Dtoken%26client_id%3D123456789012345%26ret%3Dlogin&lwv=100
this is the same URL, that is given by br.submit() command.
unfortunately Firefox is performing another redirect, first to:
https://www.facebook.com/v2.0/dialog/oauth?redirect_uri=https%3A%2F%2Fwww.facebook.com%2Fconnect%2Flogin_success.html&response_type=token&client_id=123456789012345&ret=login&ext=143846220&hash=KefDdKCLQeJo_hJR
followed by the final destination:
https://www.facebook.com/connect/login_success.html#access_token=CAAGXPLMhY0cYMlFbAPXJnPKtEPPQCP4NNZCyJkiRLE1B0iHeK6KonmgsyrzQu4uJbfe1TDV1YrmtyFb3SuYJAVhZBazHDr49oYh3i5WThZAfgTDmtHFsmpvn3ZBpeJ3NR718Dn9rm0PX4ZCpsBA6wAItUdHDcAiOZBIZACliuVEefqseFXtqCNFN2ZC9UGslohsZD&expires_in=3780
I don't know why mechanize is not performing this redirect. Any ideas how to solve this?
I have just started using python and mechanize and I need some help understanding why this will not work.
import mechanize
username = "<username>"
password = "<password>"
ua = 'Mozilla/5.0 (X11; Linux x86_64; rv:18.0) Gecko/20100101 Firefox/18.0 (compatible;)'
br = mechanize.Browser()
br.set_handle_robots(False)
br.addheaders = [('User-Agent', ua), ('Accept', '*/*')]
br.open("https://www.instagram.com/accounts/login/")
br.select_form(nr=0)
br[username] = username
br[password] = password
result = br.submit().read()
f = open('output.html','w')
f.write(result)
f.close()
the error it throws is:
FormNotFoundError: no form matching nr 0
and I cannot figure out what the correct form id is. Even after attempting to list all forms using the following:
print("\n Printing all forms on page...\n")
for form in br.forms():
print("Form Name:", form.name)
print form
print("\n Done printing forms...\n")
the list of forms is empty. How can I find what the forms id is if mechanize cannot see the form itself?
To select the first form, you could do this:
br.form = list(br.forms())[0]
then, to enter the username and password:
br.form["username"] = username
br.form["password"] = password
and then finally, to submit:
req = br.submit()
edit: just noticed you said the list of forms would return empty. This is most likely be because the site is using javascript. You might have better luck with selenium.
Good luck!
I am new to Python and trying to crawling the some information on my school website (based on aspx).
What I am trying to do is:
http://url.of.the.page
Login
Open the 4th link on the left
I was trying to log into my account by using req = urllib2.Request(url,data) (the data contains the id, password and some other information I can see through wireshark) together with result = opener.open(req) and print result.read().
Unfortunately the result printed out is the same as the original login page, so I guess I did not login successfully, this result is also same as when I click the 4th lick without login.
(Another proof is that when I wanted to get another link on the web page, I was redirected to the login page).
My question will be:
Do I really fail to login?
If so what is the correct way to login?
My code is as follow:
# -*- coding: utf-8 -*-
import urllib2
import urllib
import cookielib
from bs4 import BeautifulSoup
import datetime
import time
from urlgrabber.keepalive import HTTPHandler
def get_ViewState(soup):
view_input = soup.find(id="__VIEWSTATE")
return (view_input['value'])
def get_EventValidation(soup):
event_input = soup.find(id="__EVENTVALIDATION")
return event_input['value']
cookie = cookielib.CookieJar()
keepalive_handler = HTTPHandler()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie),keepalive_handler)
urllib2.install_opener(opener)
__url = 'http://url.of.the.page'
opener.addheaders = [('User-agent','Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.104 Safari/537.36')
,('Connection', 'Keep-Alive')
,('Referer',__url)]
page = urllib.urlopen(__url).read()
soup = BeautifulSoup(page)
viewstate = get_ViewState(soup)
eventvalidation = get_EventValidation(soup)
postdata = urllib.urlencode({
'__EVENTTARGET':'',
'__EVENTARGUMENT:':'',
'__VIEWSTATE':viewstate,
'TxtStudentId':'xxxxxxx',
'TxtPassword':'xxxxxxx',
'BtnLogin':'login',
'__EVENTVALIDATION':eventvalidation
})
req = urllib2.Request(
url = __url,
data = postdata
)
result = opener.open(req)
print result.read()
# result = opener.open(req)
# print result.info()
# print result
# print result.read()
print "------------------------------------------------"
#after login, I need to get the scores table
__queryUrl = 'http://url.of.the.page?key=0'
now = datetime.datetime.now()
opener.addheaders = [('Referer', 'http://url.of.the.page?i='+now.strftime('%H:%M:%S'))]
result = opener.open(__queryUrl)
print result.read()
for item in cookie:
print 'Cookie:Name = '+item.name
print 'Cookie:Value = '+item.value
For logging in you will have to use a Programming API for the website because it will probably ask if you are a robot. For clicking the fourth link, simply view the source code (HTML) of the website and find the class and ID of the link you want. Then, with some Googling, you can add that to the code and you are all set :)
By capturing the packages I found out that my POST message get an OK message from the server, that means I did login successfully.
The reason why GET message got a 302 found as a return is because I didn't include a cookie in the header. I was using urllib2 and it did not include the cookie in the GET message automatically.
So I hard coded the cookie into the header by doing the following:
cookie = cookielib.CookieJar()
ckName = ''
ckValue = ''
for item in cookie:
ckName = item.name
ckValue = item.value
opener.addheaders = [('User-agent','Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.104 Safari/537.36')
,('Referer', 'http://202.120.108.14/ecustedu/K_StudentQuery/K_StudentQueryLeft.aspx?i='+now.strftime('%H:%M:%S'))
,('Cookie',ckName+'='+ckValue)]
I'm trying to use Mechanize to get emails from my Outlook web client, but I'm having troubles logging in. It gives me the errors listed below. I've verified that the user name and password are correct. Any ideas?
Here is my code:
import mechanize
b = mechanize.Browser()
cj = cookielib.LWPCookieJar()
b.set_cookiejar(cj)
b.open('https://mail.example.com/owa/')
br.select_form("logonForm")
b['username'] = 'myname'
b['password'] = 'password'
b.submit()
I can see that form components are being accessed correctly, but after submitting, the login page displays again with two errors:
The user name or password you entered isn't correct. Try entering it again.
Please enable cookies for this Web site.
I thought the b.set_cookiejar(cj) would take care of cookies. Could this be the root of my problem?
import mechanize
import cookielib
br = mechanize.Browser()
br.set_handle_robots( False )
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
br.open('https://webmail.server.com')
br.select_form(nr = 0)
br.form['username'] = 'username'
br.form['password'] = 'password'
br.submit()
Use this it works for me
I've got a script set to log into a website. The challenge is that I'm running the script on EC2 and the website is asking for me to do additional verification by sending me a custom code.
I receive the email immediately but need to be able to update that field on the fly.
This is the script
import urllib2
import urllib2
import cookielib
import urllib
import requests
import mechanize
from bs4 import BeautifulSoup
# Browser
br = mechanize.Browser()
# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
# Browser options
br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_refresh(False)
br.set_handle_referer(True)
br.set_handle_robots(False)
# Follows refresh 0 but not hangs on refresh > 0
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
# User-Agent (this is cheating, ok?)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
# The site we will navigate into, handling it's session
br.open('https://www.website.com/login')
#select the first form
br.select_form(nr=0)
#user credentials
br['user_key'] = 'username#gmail.com'
br['user_password'] = 'somePassw0rd'
# Login
br.submit()
#enter verification code
input_var = raw_input("Enter something: ")
#put verification code in form
br['Verication'] = str(input_var)
#submit form
br.submit()
The challenge for me is that I keep getting an error saying:
AttributeError: mechanize._mechanize.Browser instance has no attribute __setitem__ (perhaps you forgot to .select_form()?)
What can I do to make this run as intended?
after you br.submit() you go straight into
br['Verication'] = str(input_var)
this is incorrect since using br.submit() will make your browser not have a form selected anymore.
after submitting i would try:
for form in br.forms():
print form
to see if there is another form to be selected
read up on the html code on the login site and check to see exactly what happens when you click login. You may have to reselect a form on that same page then assign the verification code to one of the controls