Is there any way of to login via the browser to facebook and google, but without using the provided APIs? So far I have tried mechanize
with cookielib
, webbrowser
, requests
and selenium
, but I did not get any satisfying results. The closest I got was to log in using mechanize + cookielib
but via command line. I have an app and all what I want it to do is to open a browser and open either the facebook or the google page and to automatically log me in using the provided user name and password.
Note: if this can be done more easily in a different language, I am interested in those too.
Thanks in advance for your help!
import urllib2,cookielib
def TryToLoginFB(username,password):
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookielib.CookieJar()))
url1 = "https://login.facebook.com"
url2 = "https://login.facebook.com/login.php?login_attempt=1"
data = "&email="+username+"&pass="+password
socket = opener.open(url1)
socket = opener.open(url2,data)
return socket
socket = TryToLoginFB("not_gonna_tell_you@gmail.com","my_password")
if "logout" in socket.read():
print "OK"
else:
print "Error"
# or use 'socket' in order to do whatever you wanna do at this point...
from selenium import webdriver
def TryToLoginFB(username,password):
browser = webdriver.Firefox()
browser.get('https://www.facebook.com')
browser.find_element_by_xpath('//input[@id="email"]').send_keys(username)
browser.find_element_by_xpath('//input[@id="pass"]').send_keys(password)
browser.find_element_by_xpath('//input[@value="Log In"]').click()
return browser
browser = TryToLoginFB("not_gonna_tell_you@gmail.com","my_password")
if "logout" in browser.page_source:
print "OK"
else:
print "Error"
# or use 'browser' in order to do whatever you wanna do at this point...
In order to install Selenium for Python on your machine, run 'pip install selenium' from a command line.
import smtplib,ssl
def TryToLoginEM(username,password):
server = Connect(username)
try:
server.login(username,password)
except smtplib.SMTPException,error:
print error
Disconnect(server)
return None
return server
def Connect(username):
serverName = username[username.index("@")+1:username.index(".")]
while True:
try:
server = smtplib.SMTP(serverDict[serverName])
except smtplib.SMTPException,error:
print error
continue
try:
server.ehlo()
if server.has_extn("starttls"):
server.starttls()
server.ehlo()
except (smtplib.SMTPException,ssl.SSLError),error:
print error
Disconnect(server)
continue
break
return server
def Disconnect(server):
try:
server.quit()
except smtplib.SMTPException,error:
print error
serverDict = {
"gmail" :"smtp.gmail.com",
"hotmail":"smtp.live.com",
"yahoo" :"smtp.mail.yahoo.com"
}
server1 = TryToLoginEM("your_email@gmail.com","your_password")
server2 = TryToLoginEM("your_email@hotmail.com","your_password")
server3 = TryToLoginEM("your_email@yahoo.com","your_password")
if server1 and server2 and server3:
print "OK"
else:
print "Error"
# or use 'server1/2/3' in order to do whatever you wanna do at this point...
Just follow the instructions for 'Log-In Facebook / Option #2'.
In order to find the xpath of the elements, open the page in a web-browser and inspect each element.