{"id":6426,"date":"2020-06-21T13:40:07","date_gmt":"2020-06-21T13:40:07","guid":{"rendered":"https:\/\/www.askpython.com\/?p=6426"},"modified":"2020-06-21T13:42:08","modified_gmt":"2020-06-21T13:42:08","slug":"python-automate-facebook-login","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/python-automate-facebook-login","title":{"rendered":"Using Selenium in Python to Automate Facebook Login"},"content":{"rendered":"\n<p>Hello everyone! In today&#8217;s article, we&#8217;ll be looking at using Python to automatically login to Facebook.<\/p>\n\n\n\n<p>This will be a fun experiment that will give you a glimpse into web-browser automation using Python&#8217;s Selenium web driver. So let&#8217;s get right into the topic and create a script that will visit the Facebook page, enter the credentials, and log right in!<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>Now, before going though this tutorial, you&#8217;ll need to install certain libraries in Python. These libraries will make it very easy for us to login to the browser.<\/p>\n\n\n\n<p>We&#8217;ll be using the <em>Selenium<\/em> webdriver module in Python. This module enables us to control our web browser (Chrome \/ Firefox) using a driver program.<\/p>\n\n\n\n<p>But, to use Selenium along with our browser, we&#8217;ll need to install the drivers for that browser (Chrome\/Firefox). To install them, we&#8217;ll take the help of another Python module: <code>webdriver_manager<\/code><\/p>\n\n\n\n<p>Instead of having to download the selenium webdriver manually, you can simply import this module! This will fetch all requirements automatically for you.<\/p>\n\n\n\n<p>So now, let&#8217;s <code>pip install<\/code> the necessary packages, using the <a href=\"https:\/\/www.askpython.com\/python-modules\/python-pip\">pip<\/a> manager:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\npip install selenium\npip install webdriver_manager\n<\/pre><\/div>\n\n\n<p>Now that we&#8217;ve installed our requirements, let&#8217;s start writing the code!<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Writing our Script to Automate Facebook login<\/h2>\n\n\n\n<p>Let&#8217;s first import the necessary modules. We&#8217;ll need <code>selenium<\/code> as well as the <code>webdriver_manager<\/code>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom selenium import webdriver\nfrom webdriver_manager.firefox import GeckoDriverManager\nfrom webdriver_manager.chrome import ChromeDriverManager\nimport time\n<\/pre><\/div>\n\n\n<p>Here, I need the <code>webdriver<\/code> class of the core Selenium module. Also, since we&#8217;ll be using it with firefox\/chrome, we&#8217;ll need to load the necessary WebDrivers.<\/p>\n\n\n\n<p>Now, we&#8217;ll be using the below url to login:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nLOGIN_URL = &#039;https:\/\/www.facebook.com\/login.php&#039;\n<\/pre><\/div>\n\n\n<p>Now, we&#8217;ll implement the login functionality as a class. Let&#8217;s call it <code>FacebookLogin<\/code>.<\/p>\n\n\n\n<p>When we call <code>__init__()<\/code>, we&#8217;ll initialize the selenium webdriver session. We need to send both the email and password fields to our webdriver session, so we&#8217;ll take them as input.<\/p>\n\n\n\n<p>Finally, we will fetch the <code>LOGIN_URL<\/code> with a GET request from the webdriver.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass FacebookLogin():\n    def __init__(self, email, password, browser=&#039;Chrome&#039;):\n        # Store credentials for login\n        self.email = email\n        self.password = password\n        if browser == &#039;Chrome&#039;:\n            # Use chrome\n            self.driver = webdriver.Chrome(executable_path=ChromeDriverManager().install())\n        elif browser == &#039;Firefox&#039;:\n            # Set it to Firefox\n            self.driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())\n        self.driver.get(LOGIN_URL)\n        time.sleep(1) # Wait for some time to load\n<\/pre><\/div>\n\n\n<p>Alright, now we have initialized the class instance. Now, to login, we&#8217;ll create another method called <code>login()<\/code> to do this for us.<\/p>\n\n\n\n<p>To login, we&#8217;ll need to give the input to the login elements (<code>email<\/code> and <code>pass<\/code> on the html page)<\/p>\n\n\n\n<p>Selenium has the <code>find_element_by_id()<\/code> method, which will automatically locate the corresponding element for you!<\/p>\n\n\n\n<p>To send the keyboard input, we can use <code>element.send_keys(input)<\/code> directly!<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n    def login(self):\n        email_element = self.driver.find_element_by_id(&#039;email&#039;)\n        email_element.send_keys(self.email) # Give keyboard input\n\n        password_element = self.driver.find_element_by_id(&#039;pass&#039;)\n        password_element.send_keys(self.password) # Give password as input too\n\n        login_button = self.driver.find_element_by_id(&#039;loginbutton&#039;)\n        login_button.click() # Send mouse click\n\n        time.sleep(2) # Wait for 2 seconds for the page to show up\n<\/pre><\/div>\n\n\n<p>Notice how simple the API is! We can directly do <code>element.send_keys()<\/code> and <code>element.click()<\/code>!<\/p>\n\n\n\n<p>Finally, give the program some time to load the webpage, using <code>time.sleep()<\/code><\/p>\n\n\n\n<p>I&#8217;ll give you the full code below. Just make sure to use your proper login credentials in the <code>main<\/code> module.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom selenium import webdriver\nfrom webdriver_manager.firefox import GeckoDriverManager\nfrom webdriver_manager.chrome import ChromeDriverManager\nimport time\n\n\nLOGIN_URL = &#039;https:\/\/www.facebook.com\/login.php&#039;\n\nclass FacebookLogin():\n    def __init__(self, email, password, browser=&#039;Chrome&#039;):\n        # Store credentials for login\n        self.email = email\n        self.password = password\n        if browser == &#039;Chrome&#039;:\n            # Use chrome\n            self.driver = webdriver.Chrome(executable_path=ChromeDriverManager().install())\n        elif browser == &#039;Firefox&#039;:\n            # Set it to Firefox\n            self.driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())\n        self.driver.get(LOGIN_URL)\n        time.sleep(1) # Wait for some time to load\n\n\n\n    def login(self):\n        email_element = self.driver.find_element_by_id(&#039;email&#039;)\n        email_element.send_keys(self.email) # Give keyboard input\n\n        password_element = self.driver.find_element_by_id(&#039;pass&#039;)\n        password_element.send_keys(self.password) # Give password as input too\n\n        login_button = self.driver.find_element_by_id(&#039;loginbutton&#039;)\n        login_button.click() # Send mouse click\n\n        time.sleep(2) # Wait for 2 seconds for the page to show up\n\n\nif __name__ == &#039;__main__&#039;:\n    # Enter your login credentials here\n    fb_login = FacebookLogin(email=&#039;sample@example.com&#039;, password=&#039;PASSWORD&#039;, browser=&#039;Firefox&#039;)\n    fb_login.login()\n<\/pre><\/div>\n\n\n<p>Hopefully your browser will be showing your home page now. Hurray, you&#8217;ve successfully logged on to facebook!<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this article, we learned about using Python and Selenium to automate logging onto Facebook quickly!<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n","protected":false},"excerpt":{"rendered":"<p>Hello everyone! In today&#8217;s article, we&#8217;ll be looking at using Python to automatically login to Facebook. This will be a fun experiment that will give you a glimpse into web-browser automation using Python&#8217;s Selenium web driver. So let&#8217;s get right into the topic and create a script that will visit the Facebook page, enter the [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":6428,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-6426","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/6426","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=6426"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/6426\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/6428"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=6426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=6426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=6426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}