**Python Selenium automates browsers and controls their activities. We can write code to control browser tasks using python Selenium. It is primarily used for automating web applications for testing purposes. but it is certainly not limited to just that. It can also Automate Repetitive Tasks on the web. The more you learn. the more enjoyable it becomes to see things happen automatically and save time instead of repeatedly performing monotonous tasks. Here, we use python Selenium to open the website we need we need to Automate Facebook in this case and then inspect the elements through the email field, password field. and login button to find their identifiers.**
![]() |
Automate Facebook Using a Python Selenium Bot part 1 |
Using find_element(By. ID) and find_element(By. NAME) provided by the Selenium module, we can locate the desired elements (username field, password field, login button) and interact with them. By using the send_keys() method provided by the Selenium module, we can input data into these fields.
Installing Required Libraries
pip/pip3 install selenium
import libraries
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
import os
from time import sleep
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome( options=options)
Selenium: Browser Automation
Time: To pause the program for several seconds as browsers try to detect automation if data is entered too quickly.
Get Username and Password Inputs:
Use the input() function to take the username and password as inputs from the user, passing a prompt message as an argument.
Open the Browser and Target Website:
The webdriver.Chrome() function will open a new window for the Chrome browser. We will store its object in a variable called driver. Then, using the get method, we will navigate to the Facebook website.
Find the Element to Send Data and Submit Inputs:
Use the browser's element inspector tool to find the identifiers of the elements you want to interact with. In this case, we will inspect the username field, password field, and login button to find their IDs. We then use these IDs with python Selenium find_element method to locate the elements on the web page and store them in variables for later use. Using the send_keys() method, we will input the data into the previously found elements.
Close the Browser:
After completing the above steps, we need to end the session using the driver.quit() method. Note: Here, the driver is the name of the variable chosen for the web driver.Chrome().
Full Code:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
import os
from time import sleep
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome( options=options)
driver.get('https://www.facebook.com/')
print ("Opened facebook")
sleep(1)
username_box = driver.find_element(By.ID, 'email')
username_box.send_keys(usr)
print ("Email Id entered")
sleep(1)
password_box = driver.find_element(By.ID, 'pass')
password_box.send_keys(pwd)
print ("Password entered")
login_box = driver.find_element(By.NAME, 'login')
login_box.click()
print ("Done")
driver.get('')