3

I want to know how to log out of Facebook using python selenium. here's my code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
#from selenium folder include webdriver folders

driver=webdriver.Firefox()
#Using this, we create the Firefox instance of Selenium webdriver.
driver.maximize_window()
driver.get("http://www.facebook.com")
assert "Facebook" in driver.title
user=driver.find_element_by_id("email")
user.clear()
user.send_keys("sekar2225@gmail.com")

passw=driver.find_element_by_id("pass")
passw.clear()
passw.send_keys("emmawatson1")

passw.send_keys(Keys.RETURN)

logout=driver.find_element_by_id("userNavigationLabel")
logout.click()
logout1=driver.find_element_by_class_name("uiLinkButtonInput") #error in this line
logout1.click()

time.sleep(5)

driver.quit()
2
  • When looking for help on a forum please make it a habit to format the text and code properly! Writing everything in a single line without any punctuation is difficult to read and understand. Jul 18 '16 at 4:17
  • you can use selenium to locate the logout button.
    – Yu Zhang
    Jul 18 '16 at 4:30
1

You can use "find_element_by_css_selector" instead of "find_element_by_class_name".

Check out my blog for the working code for face book login and logout: http://pythoninterface.blogspot.in/2016/10/automation-for-login-and-logout-of.html

1
  • 1
    The blogspot.in website that you reference is dead. It cannot be found by search engines. Please update your link. Thank you. Dec 14 '20 at 5:01
1

According to the code which you have used, the error line can be solved using this below. The problem is that facebook keeps on changing this element from time to time. So may be after some time or so it won't work. But this can be resolved if you know basic web development. Just find the class name and action when it gets changed. Hope it works.

logout1 = driver.find_element_by_css_selector("._w0d[action='https://www.facebook.com/logout.php?button_name=logout&button_location=settings']").submit()
0

Please use this code:

logout1 = driver.find_element_by_partial_link_text('Log Out')
logout1.click()

facebook logout work done...

0

It is not working because Button "Log Out" needs some loading time to be shown in website.
So use this it will definitely work for all. Sleeping time should be given to load text inside that button after clicking the button and before clicking on log out

From selenium import webdriver
import time:

User_Name = input(">")
Pass_Word = input(">")
if len(User_Name) > 1 and len(Pass_Word) > 1:
    browser = webdriver.Chrome()
    browser.get("https://facebook.com")
    sign_in = browser.find_element_by_link_text("Log In")
    sign_in.click()
    username = browser.find_element_by_id("email")
    username.send_keys(User_Name)
    password = browser.find_element_by_id("pass")
    password.send_keys(Pass_Word)
    password.submit()
    button = browser.find_element_by_id("userNavigationLabel")
    button.click()
    time.sleep(20)
    l_og = browser.find_element_by_link_text("Log Out")
    l_og.click()
    browser.close()
else:
    raise KeyError
1
0

An alternative solution would be to just delete the Facebook cookie, assuming you're not testing the logout button, but just want to logout or clear your session.

driver.delete_all_cookies()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.