{"id":3637,"date":"2020-11-15T17:17:44","date_gmt":"2020-11-15T11:47:44","guid":{"rendered":"https:\/\/copyassignment.com\/?p=3637"},"modified":"2022-06-18T17:11:16","modified_gmt":"2022-06-18T11:41:16","slug":"automate-facebook-login-using-python-selenium","status":"publish","type":"post","link":"https:\/\/copyassignment.com\/automate-facebook-login-using-python-selenium\/","title":{"rendered":"Automate Facebook Login Using Python Selenium"},"content":{"rendered":"\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p>In this article, we will <strong><span class=\"has-inline-color has-vivid-red-color\">Automate Facebook Login Using Python<\/span><\/strong>, and to do this, we need to install one external module i.e. <strong><span class=\"has-inline-color has-vivid-red-color\">selenium<\/span><\/strong> which is best for this type of kind of stuff.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Selenium<\/h2>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p>&#8220;Selenium automates browsers. That&#8217;s it! What you do with that power is entirely up to you. Primarily it is for automating web applications for testing purposes but is certainly not limited to just that&#8221; &#8212; from selenium website.<\/p>\n\n\n\n<p>We can use selenium in many languages like Java and Python. Here, we will use selenium to Automate Facebook Login Using Python.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Installations\/Downloads<\/h2>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>There are two requirements&#8211;<\/strong><\/h3>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">1. <strong><a rel=\"noreferrer noopener\" href=\"https:\/\/pypi.org\/project\/PyAudio\/\" target=\"_blank\">Selenium<\/a><\/strong>&#8212; to install Selenium, go to terminal and type,<\/h4>\n\n\n\n<div class=\"maindiv\"><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">pip install selenium<\/pre><\/strong>\n<\/div><\/div>\n\n\n\n<p>To install selenium in PyCharm, see the picture&#8211;<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n\n\n<h4 class=\"wp-block-heading\">2. <strong><a rel=\"noreferrer noopener\" href=\"https:\/\/chromedriver.chromium.org\/downloads\" target=\"_blank\">ChromeDriver<\/a><\/strong>&#8212; to download chromedriver, <a rel=\"noreferrer noopener\" href=\"https:\/\/chromedriver.chromium.org\/downloads\" target=\"_blank\">click here<\/a>.<\/h4>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p><strong><span class=\"has-inline-color has-vivid-red-color\">ChromeDriver <\/span><\/strong>or any other browser&#8217;s driver like firefox also needs to be downloaded by us because when we want to automate something with selenium, these drivers are necessary, without them, we can not open any browser, and if we can not open a browser then we can not automate anything, Right!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<script async=\"\" src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\" style=\"display:block; text-align:center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-9886351916045880\" data-ad-slot=\"2002566052\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p>Before you download the chromedriver, keep in mind that the version of the chromedriver you are downloading should match with the version of the Chrome Browser which is installed in your system.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p>To check the version of your Chrome Browser, follow the given steps&#8211;<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p><strong>1.  Open your Chrome Browser<\/strong><\/p>\n\n\n\n<p><strong>2. Click on three dots available at the top-right position of the Browser.<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p><strong>3. Then click on &#8220;Help&#8221;  and then finally click on &#8220;About Google Chrome&#8221;.<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p><strong>4. Then you will see the installed version of Chrome installed on your system.<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p>5. Here, our version is &#8220;<strong><span class=\"has-inline-color has-vivid-red-color\">86.0.4240.198<\/span><\/strong>&#8220;, so we will download this version from <a rel=\"noreferrer noopener\" href=\"https:\/\/chromedriver.chromium.org\/downloads\" target=\"_blank\">that<\/a> website.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n\n\n<p>You should check your Chrome Browser&#8217;s version and download it as shown here.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p>Now, we are all setup to Automate Facebook Login Using Python or anyother things you want to automate with Python.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<script async=\"\" src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\" style=\"display:block; text-align:center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-9886351916045880\" data-ad-slot=\"2002566052\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p>So, let&#8217;s code for &#8220;the <strong><span class=\"has-inline-color has-vivid-red-color\">automate facebook login using Python<\/span><\/strong>&#8220;.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p>First, import webdrivers from selenium as follows&#8211;<\/p>\n\n\n\n<div class=\"maindiv\"><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">from selenium import webdriver<\/pre><\/strong>\n<\/div><\/div>\n\n\n\n<p>Now, import Keys&#8211;<\/p>\n\n\n\n<div class=\"maindiv\"><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">from selenium.webdriver.common.keys import Keys<\/pre><\/strong>\n<\/div><\/div>\n\n\n\n<p>Now, import time module&#8211;<\/p>\n\n\n\n<div class=\"maindiv\"><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">import time<\/pre><\/strong>\n<\/div><\/div>\n\n\n\n<p>After all imports, we will indicate the user that the program has started&#8211;<\/p>\n\n\n\n<div class=\"maindiv\"><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">print(\"test case started\")<\/pre><\/strong>\n<\/div><\/div>\n\n\n\n<p>Now we will open our Chrome Browser as follows&#8211;<\/p>\n\n\n\n<div class=\"maindiv\"><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">driver = webdriver.Chrome()<\/pre><\/strong>\n<\/div><\/div>\n\n\n\n<p>Here, inside the Chrome(), you need to give the path of the chromedriver which you have installed, or you can just copy and paste the chromedriver in the same folder\/directory where this program(you are coding in) exists and leave the Chrome() as it is.<\/p>\n\n\n\n<p>If you want to maximize the windows automatically, then type&#8211;<\/p>\n\n\n\n<div class=\"maindiv\"><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">driver.maximize_window()<\/pre><\/strong>\n<\/div><\/div>\n\n\n\n<p>As we have opened the Chrome Browser, now we need to open the facebook login page, so to open any url, we just need to write&#8211;<\/p>\n\n\n\n<div class=\"maindiv\"><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">driver.get(\"url\")<\/pre><\/strong><\/div>\n\n\n\n<p>We need to open the facebook login page, so we will type&#8211;<\/p>\n\n\n\n<div class=\"maindiv\"><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">driver.get(\"https:\/\/www.facebook.com\")<\/pre><\/strong>\n<\/div><\/div>\n\n\n\n<p>After this, the login page will get opened automatically.<\/p>\n\n\n\n<p>So, now we need to enter the login creditionals i.e. email-id and password Right! and after that we need to click on the &#8220;Log In&#8221; button. Also, we want all this to be done automatically.<\/p>\n\n\n\n<p>Here comes the basic HTML part in role i.e. if you know the basics of HTML then it will be a benefit for you to <strong>automate Facebook login using Python<\/strong>.<\/p>\n\n\n\n<p>If not, let me do this for you, selenium uses HTML elements to define blocks of different types like paragraph, headings, horizontal line, and many more like this. To know more detailed information about HTML elements, <a href=\"https:\/\/www.w3schools.com\/html\/html_elements.asp\" target=\"_blank\" rel=\"noreferrer noopener\">click here<\/a>.<\/p>\n\n\n\n<p>Every HTML elements have some attributes. An attribute is used to define the characteristics of an HTML element and is placed inside the element&#8217;s opening tag.<\/p>\n\n\n\n<p>We use HTML attributes to target the elements for example, to enter email-id inside &#8220;email id&#8221; block on the facebook login page, we need it one of the available attributes like name, id, class, etc.<\/p>\n\n\n\n<p>If we get the attribute of that element, we can pass values to that element accordingly and if the element is a button, then also we can use its attribute to click on that button.<\/p>\n\n\n\n<p>In selenium with python, we have many ways to reach any element by using the following attributes&#8211;<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<ol class=\"myfontsize wp-block-list\"><li>find_element_by_xpath<\/li><li>find_element_by_class_name<\/li><li>find_element_by_id<\/li><li>find_element_by_name<\/li><li>find_element_by_link_text<\/li><li>find_element_by_css_selector<\/li><\/ol>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p>And many more.<\/p>\n\n\n\n<p>Now, you will be wondering, how to get those attributes or the other things?<\/p>\n\n\n\n<p>This is very simple, go to the Facebook login page, right-click on the page anywhere, and then click on the &#8220;Inspect&#8221; available at the bottom.<\/p>\n\n\n\n<p>Then, you will see an icon available which contains the arrow same as your mouse arrow on your screen, in the newly opened box, just click on that icon.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p><strong>After that, take your mouse above the &#8220;email&#8221; block and click on it.<\/strong><\/p>\n\n\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p>Now, on the right side, you can see the available attributes of that &#8220;email&#8221; block and use them in your automation program.<\/p>\n\n\n\n<p>In the same way, you can get any element&#8217;s attribute.<\/p>\n\n\n\n<p>In this program, we will use XPath which is considered best for these types of uses. To get the XPath of any element, simply right click on that elements highlighted part in the inspect element, then take your mouse to &#8220;copy&#8221; and then click on &#8220;Copy XPath&#8221;.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p>Now, to fill that email-id block we need to type&#8211;<\/p>\n\n\n\n<div class=\"maindiv\"><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">driver.find_element_by_xpath('XPath_you_copied').send_keys(\"email-id here\")<\/pre><\/strong>\n<\/div><\/div>\n\n\n\n<p>In the same way, we can fill the &#8220;password&#8221; block&#8211;<\/p>\n\n\n\n<div class=\"maindiv\"><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">driver.find_element_by_xpath('XPath_you_copied').send_keys(\"password here\")<\/pre><\/strong>\n<\/div><\/div>\n\n\n\n<p>Now, as we have entered the correct details, we need to click on the &#8220;Log In&#8221; button. For this we need to type&#8211;<\/p>\n\n\n\n<div class=\"maindiv\"><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">driver.find_element_by_xpath('XPath_you_copied').send_keys(Keys.ENTER)<\/pre><\/strong>\n<\/div><\/div>\n\n\n\n<p>Now, we will close the browser using&#8211;<\/p>\n\n\n\n<div class=\"maindiv\"><div class=\"codediv\"><strong><pre style=\"color:#cf2e2e;\">driver.close()<\/pre><\/strong>\n<\/div><\/div>\n\n\n\n<p>Congratulation, you have successfully completed all the steps to Automate Facebook Login Using Python.<\/p>\n\n\n\n<p>Now, let;s have a look at the full code.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Code<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><div style=\"position:absolute;top:-20px;right:0px;cursor:pointer\" class=\"copy-simple-code-block\"><svg aria-hidden=\"true\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"20\" height=\"20\" viewbox=\"0 0 20 20\" class=\"dashicon dashicons-admin-page\"><path d=\"M6 15V2h10v13H6zm-1 1h8v2H3V5h2v11z\"><\/path><\/svg><\/div><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"true\">from selenium import webdriver\nimport time\nfrom selenium.webdriver.common.keys import Keys\nprint(\"test case started\")\n#open Google Chrome browser\ndriver = webdriver.Chrome()\n#maximize the window size\ndriver.maximize_window()\n#delete the cookies\ndriver.delete_all_cookies()\n#navigate to the url\ndriver.get(\"https:\/\/www.facebook.com\")\ndriver.find_element_by_xpath('XPath_you_copied').send_keys(\"email-id here\")\ntime.sleep(1)\ndriver.find_element_by_xpath('XPath_you_copied').send_keys(\"password here\")\ntime.sleep(1)\ndriver.find_element_by_xpath('XPath_you_copied').send_keys(Keys.ENTER)\ntime.sleep(1)\n#close the browser\ndriver.close()\nprint(\"facebook login has been successfully completed\")<\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p class=\"has-large-font-size\"><strong>Also Read:<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p><a href=\"https:\/\/copyassignment.com\/create-language-translator-using-python\/\">Create Language Translator Using Python<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/copyassignment.com\/covid-19-tracker-application-using-python\/\">Covid-19 Tracker Application Using Python<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/copyassignment.com\/youtube-downloader-application-using-python\/\">YouTube Video Downloader Application Using Python<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/copyassignment.com\/gui-calendar-using-python\/\">GUI Calendar using Python<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/copyassignment.com\/gui-application-to-check-wifi-password-in-python\/\">GUI Application To See wifi password in Python<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/copyassignment.com\/get-wifi-passwords-with-python\/\">Get WiFi Passwords With Python<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will Automate Facebook Login Using Python, and to do this, we need to install one external module i.e. selenium which is&#8230;<\/p>\n","protected":false},"author":1,"featured_media":12742,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,1403],"tags":[1686,1085,1087,1684],"class_list":["post-3637","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-allcategorites","category-python-projects","tag-automate-facebook-login-using-python-selenium","tag-python-web-scraping","tag-selenium","tag-web-scraping","wpcat-22-id","wpcat-1403-id"],"_links":{"self":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts\/3637","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/comments?post=3637"}],"version-history":[{"count":0,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts\/3637\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/media\/12742"}],"wp:attachment":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/media?parent=3637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/categories?post=3637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/tags?post=3637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}