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("[email protected]")
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()
Mate Mrše
4,1194 gold badges25 silver badges51 bronze badges
asked Jul 18, 2016 at 4:11
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. Commented Jul 18, 2016 at 4:17
  • you can use selenium to locate the logout button. Commented Jul 18, 2016 at 4:30

5 Answers 5

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

answered Oct 13, 2016 at 11:16
1
  • 1
    The blogspot.in website that you reference is dead. It cannot be found by search engines. Please update your link. Thank you. Commented Dec 14, 2020 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()
answered May 10, 2017 at 18:21
0

Please use this code:

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

facebook logout work done...

alecxe
11.4k11 gold badges52 silver badges107 bronze badges
answered Jan 1, 2019 at 16:31
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
Bharat Mane
6,78512 gold badges42 silver badges69 bronze badges
answered Dec 23, 2019 at 5:22
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()
answered Jan 14, 2021 at 17:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.