Thursday, 13 October 2016

Automation for login and logout of Facebook using Python.

Basic setup required:

First we need to do basic setup of python and the "selenium" module installation.

For basic python setup see the following video's:
Installation of selenium module using pip:

For installing a python module see the following video : https://www.youtube.com/watch?v=5zEQaYBukz4

pip install selenium


Following code is used to login and logout from a Facebook account:

# Modules to be imported.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

# Your login details.
email = "<email>@gmail.com"
passwd = "<your password>"

# Browser : Firefox.
driver = webdriver.Firefox()
driver.get("https://www.facebook.com")
time.sleep(2)

# Facebook login.
username=driver.find_element_by_id("email")
username.send_keys(email)
time.sleep(2)
password=driver.find_element_by_id("pass")
password.send_keys(passwd)
time.sleep(2)
login=driver.find_element_by_id("loginbutton")
login.click()

# Facebook logout.
logout1=driver.find_element_by_css_selector("#userNavigationLabel")
logout1.click()
time.sleep(2)
logout2=driver.find_element_by_css_selector("li._54ni:nth-child(12) > a:nth-child(1) > span:nth-child(1) > span:nth-child(1)")
logout2.click()

# Closing Browser.
driver.quit()

Note:

Keep seeing my blog for different automation thing that we do manually in our daily life.
My next post will be on "GMAIL login and logout automation".

9 comments:

  1. Looks great but could you please provide explanation for logout2=driver.find_element_by_css_selector("li._54ni:nth-child(12) > a:nth-child(1) > span:nth-child(1) > span:nth-child(1)") what is the meaning those numbers in brackets? Thank you in advance

    ReplyDelete
    Replies
    1. That is a Unique Selector.
      Also i am planning to do a video on it this weekend.
      So that i can provide you the link in the same post.

      Delete
  2. Nice information thank you,if you want more information please visit our link selenium Online Training

    ReplyDelete
  3. Please can you give me this your blog, i will manage it , i will give some thong other like domain etc

    ReplyDelete
  4. Good to know about the email list business. I was looking for such a service for a long time o grow my local business but the rates that other companies were offering were not satisfactory. Thanks for sharing the recommendations in this post.DataScience with Python Training in Bangalore

    ReplyDelete