Facebook Login using Python





Python scripting is one of the most interesting and intriguing activities in the interim learning Python. Automation and controlling program is one of them.





In this specific article, we will perceive how to sign into Facebook record utilizing Python and intensity of selenium.





Selenium automates and controls programs and it's movement. We can code in our manner to control program assignments with the assistance of selenium. Basically, it is for mechanizing web applications for testing purposes, however, is unquestionably not restricted to simply that. Exhausting online organization errands can be computerized too. As you adapt more it's such a great amount of amusing to see things happening naturally and sparing time in doing pointless errands over and over.





We use selenium here to open the site of our necessity (for this situation Facebook) and there we assess component crosswise over email box, secret word box and login catch to discover id of them.





· Using find_element_by_id() work given by selenium module, we can locate the required component (username box, secret key box, login catch)

· Using send_keys() work, given by selenium module, we will send the information into the container.





1. Installing third party modules required

Selenium

get pass

Additional Requirement: geckodriver for firefox and

chrome driver for chrome





2. Importing necessary modules





· Selenium: to automate browser

· Time: to pause running of the script for some seconds as browsers try to detect automation stuff if we input too fast





3. Taking username and password as input from the user

Using input() function and passing the prompt message as an argument.





4. Opening the browser and required website

webdriver.Chrome() will open a new window of chrome. We will spare it an item in a factor named driver.

Presently utilizing get work we will open up the Facebook site.





5. Finding the element for sending data and Sending input

Use assess component apparatus on the component of the program of which you need to discover id. For this situation, we will examine the username box, secret phrase box, login catch to discover their id. And afterwards, utilize this id consolidating with selenium work find_element_by_id() to discover it crosswise over the website page and spare it in factors for later use. At that point, by utilizing send_keys() we will send information over the components found beforehand.





6. Closing the browser

After the majority of the above advances we need to stop the session and will be accomplished by utilizing driver.quit().





Note: Here the driver is the name of the variable you chose for webdriver.Chrome().





Complete Code:

from selenium import webdriver

from time import sleep





usr=input('Enter Email Id:')

pwd=input('Enter Password:')





driver = webdriver.Chrome()

driver.get('https://www.facebook.com/')

print ("Opened facebook")

sleep(1)





username_box = driver.find_element_by_id('email')

username_box.send_keys(usr)

print ("Email Id entered")

sleep(1)





password_box = driver.find_element_by_id('pass')

password_box.send_keys(pwd)

print ("Password entered")





login_box = driver.find_element_by_id('loginbutton')

login_box.click()





print ("Done")

input('Press anything to quit')

driver.quit()

print("Finished")





See how such a concise piece of code can automate things for you.





Bonus:

We can likewise enter the secret word without showing it on screen, for security reason. For that, we need to incorporate one more module called getpass. Presently with only one change in info proclamation of the secret word we can enter secret phrase without showing it on screen.





from getpass import getpass

pwd = getpass('Enter Password:')





Getpass prompts the client for a secret key without resounding. Essentially it allows you to enter the secret word without indicating it on the screen.





Additionally, you can likewise computerize numerous different things like twitter login, tweeting, facebook logout and significantly more.



