Hello, future automation expert! In Module Nine, we learned how to extract data from websites using web scraping. But what if we need to interact with a website โ like clicking buttons, filling forms, or logging in? That's where Selenium comes in.
In this module, we will learn how to use Selenium to control a web browser automatically. Selenium can open websites, click links, type text, and even take screenshots. By the end of this module, you will be able to automate many tasks that you normally do in a web browser.
Grace is a student in Lagos. Every morning, she had to log in to the school portal to check her grades. She typed her username and password every day. It was boring and took time.
Her friend told her about Selenium. Grace wrote a program that opened the browser, went to the portal, entered her username and password, and clicked the login button. Now she runs the program and it does everything for her. Grace saves time every day!
Definition: Selenium is a tool that allows you to control a web browser automatically.
Why important: Selenium can automate tasks in any web application.
Simple explanation: Like a robot that uses a web browser for you.
Real-life example: Automating login to a website.
School example: Automating checking grades online.
Home example: Automating paying bills online.
Nigerian example: Automating checking bank account balance.
Illustration:
Python โ Selenium โ Browser โ Website
(code) (driver) (Chrome) (action)
Mini summary: Selenium controls a web browser automatically.
Definition: You need to install Selenium and a web driver (like ChromeDriver).
Why important: This is how you set up Selenium on your computer.
Simple explanation: Like installing a tool before you start a project.
Real-life example: pip install selenium
School example: Installing Selenium for a project.
Home example: Installing Selenium to automate tasks.
Nigerian example: Installing Selenium for business automation.
Illustration:
pip install selenium
# Download ChromeDriver from https://chromedriver.chromium.org/
Mini summary: Install Selenium and a web driver.
Definition: You start a browser using the web driver.
Why important: This opens a browser window that Selenium can control.
Simple explanation: Like opening a browser and handing it to a robot.
Real-life example: from selenium import webdriver; driver = webdriver.Chrome()
School example: Starting a browser to automate a task.
Home example: Starting a browser to automate a task.
Nigerian example: Starting a browser to automate a task.
Illustration:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://example.com")
Mini summary: Use webdriver.Chrome() to start a browser.
Definition: You use driver.get(url) to open a website.
Why important: This is how you navigate to a web page.
Simple explanation: Like typing a URL in the address bar.
Real-life example: driver.get("https://example.com")
School example: Opening the school portal.
Home example: Opening a shopping website.
Nigerian example: Opening a bank website.
Illustration:
driver.get("https://example.com")
Mini summary: Use driver.get() to open a website.
Definition: You find elements on a web page using find_element() or find_elements().
Why important: You need to find elements to interact with them.
Simple explanation: Like finding a button on a page.
Real-life example: driver.find_element("id", "username")
School example: Finding the login form.
Home example: Finding the search box.
Nigerian example: Finding the login button.
Illustration:
# Find by ID
element = driver.find_element("id", "username")
# Find by class
element = driver.find_element("class name", "btn")
Mini summary: Use find_element() to find elements.
Definition: You can interact with elements using methods like click(), send_keys(), and clear().
Why important: This is how you fill forms, click buttons, and more.
Simple explanation: Like typing and clicking on a web page.
Real-life example: element.send_keys("username")
School example: Entering a student ID.
Home example: Entering a search term.
Nigerian example: Entering a phone number.
Illustration:
username = driver.find_element("id", "username")
username.send_keys("my_username")
password = driver.find_element("id", "password")
password.send_keys("my_password")
login_btn = driver.find_element("id", "login")
login_btn.click()
Mini summary: Use send_keys(), click(), etc. to interact.
Definition: You can automate login by finding the form elements, entering data, and clicking the login button.
Why important: This is a very common automation task.
Simple explanation: Like having a robot log in for you.
Real-life example: Automating login to your email.
School example: Automating login to the school portal.
Home example: Automating login to a social media account.
Nigerian example: Automating login to a bank account.
Illustration:
driver.get("https://example.com/login")
username = driver.find_element("id", "username")
username.send_keys("my_username")
password = driver.find_element("id", "password")
password.send_keys("my_password")
login_btn = driver.find_element("id", "login")
login_btn.click()
Mini summary: Automate login with Selenium.
Definition: You can fill forms by finding input fields and sending text.
Why important: This allows you to automate data entry.
Simple explanation: Like filling an online form automatically.
Real-life example: Automating a job application.
School example: Automating a survey.
Home example: Automating a shopping order.
Nigerian example: Automating a registration form.
Illustration:
name = driver.find_element("id", "name")
name.send_keys("Aisha Bello")
email = driver.find_element("id", "email")
email.send_keys("aisha@email.com")
submit = driver.find_element("id", "submit")
submit.click()
Mini summary: Fill forms with send_keys().
Definition: You can take screenshots of the current browser window using save_screenshot().
Why important: This allows you to capture visual proof of your automation.
Simple explanation: Like taking a picture of the screen.
Real-life example: driver.save_screenshot("screenshot.png")
School example: Saving a screenshot of your grades.
Home example: Saving a screenshot of a recipe.
Nigerian example: Saving a screenshot of a transaction.
Illustration:
driver.save_screenshot("page.png")
Mini summary: Use save_screenshot() to take screenshots.
Definition: Sometimes web pages take time to load. You can use WebDriverWait to wait for elements.
Why important: This prevents errors from trying to interact with elements that haven't loaded.
Simple explanation: Like waiting for a page to finish loading.
Real-life example: WebDriverWait(driver, 10).until(EC.presence_of_element_located(("id", "username")))
School example: Waiting for the login page to load.
Home example: Waiting for the search results to load.
Nigerian example: Waiting for the bank portal to load.
Illustration:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
username = wait.until(EC.presence_of_element_located(("id", "username")))
Mini summary: Use WebDriverWait to wait for elements.
Definition: You can close the browser using driver.quit().
Why important: This ensures the browser is properly closed after automation.
Simple explanation: Like closing a browser window.
Real-life example: driver.quit()
School example: Closing the browser after checking grades.
Home example: Closing the browser after shopping.
Nigerian example: Closing the browser after a transaction.
Illustration:
driver.quit()
Mini summary: Use driver.quit() to close the browser.
Definition: Selenium can be used to automate many tasks: logging in, filling forms, extracting data, and more.
Why important: It allows you to automate tasks that require a web browser.
Simple explanation: Like having a robot that uses the internet for you.
Real-life example: Automating social media posting.
School example: Automating checking online grades.
Home example: Automating online shopping.
Nigerian example: Automating bank transactions.
Illustration:
# Automate checking grades
driver.get("https://schoolportal.com/login")
# Login
# Check grades
# Save screenshot
driver.quit()
Mini summary: Use Selenium for many automation tasks.
Definition: You have learned how to use Selenium to automate browser tasks.
Why important: You can now automate any web-based task.
Simple explanation: You can make Python use the web browser for you.
Real-life example: You can automate your daily web tasks.
School example: You can automate checking grades.
Home example: You can automate shopping.
Nigerian example: You can automate bank tasks.
Illustration:
Python + Selenium = Web Automation Power! ๐
Mini summary: You are ready to automate web tasks!
from selenium import webdriverdriver = webdriver.Chrome()driver.get("https://example.com/login")username = driver.find_element("id", "username")username.send_keys("my_username")password = driver.find_element("id", "password")password.send_keys("my_password")login_btn = driver.find_element("id", "login"); login_btn.click()import time; time.sleep(2)driver.quit()
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://example.com/login")
username = driver.find_element("id", "username")
username.send_keys("my_username")
password = driver.find_element("id", "password")
password.send_keys("my_password")
login_btn = driver.find_element("id", "login")
login_btn.click()
time.sleep(2)
driver.quit()
driver.get("https://example.com/login"); username.send_keys("user"); password.send_keys("pass"); login_btn.click()search = driver.find_element("name", "q"); search.send_keys("Python"); search.submit()name.send_keys("Aisha"); email.send_keys("aisha@email.com"); submit.click()driver.save_screenshot("page.png")WebDriverWait(driver, 10).until(EC.presence_of_element_located(("id", "username")))driver.get("https://bank.com/login"); username.send_keys("1234567890"); password.send_keys("my_password"); login_btn.click()driver.get("https://schoolportal.com"); username.send_keys("student_id"); password.send_keys("password"); login_btn.click()search = driver.find_element("name", "search"); search.send_keys("rice"); search.submit()name.send_keys("Chidi"); email.send_keys("chidi@email.com"); submit.click()driver.save_screenshot("transaction.png")driver.get("https://google.com"); search.send_keys("funny cats"); search.submit()driver.get("https://youtube.com"); search.send_keys("Python for kids"); search.submit()driver.get("https://example.com"); driver.save_screenshot("page.png")driver.get("https://example.com"); print(driver.title)driver.get("https://example.com"); driver.quit()driver.get("https://gmail.com"); username.send_keys("my_email"); password.send_keys("my_password"); login_btn.click()driver.get("https://amazon.com"); search.send_keys("books"); search.submit()driver.get("https://bbc.com"); headlines = driver.find_elements("class", "headline"); for headline in headlines: print(headline.text)driver.get("https://weather.com"); temp = driver.find_element("class", "temp"); print(temp.text)driver.get("https://twitter.com"); login_btn.click()send_keys() to type text.click() to click buttons.WebDriverWait to wait for elements.driver.quit().WebDriverWait for loading pages.
+----------------------+
| Start Browser |
| webdriver.Chrome() |
+----------------------+
|
V
+----------------------+
| Open Website |
| driver.get(url) |
+----------------------+
|
V
+----------------------+
| Find Elements |
| find_element() |
+----------------------+
|
V
+----------------------+
| Interact |
| send_keys(), click() |
+----------------------+
|
V
+----------------------+
| Close Browser |
| driver.quit() |
+----------------------+
| Method | Description |
|---|---|
driver.get(url) | Opens a URL |
driver.find_element(by, value) | Finds an element |
element.send_keys(text) | Types text |
element.click() | Clicks an element |
driver.save_screenshot(file) | Takes a screenshot |
| Selenium | BeautifulSoup |
|---|---|
| Controls browser | Parses HTML |
| Can interact with pages | Only extracts data |
| Slower | Faster |
| Handles JavaScript | Doesn't handle JavaScript |
In this module, we learned how to use Selenium to automate browser tasks. We discovered that Selenium can control a web browser, allowing us to interact with websites automatically. We learned how to start a browser, open websites, find elements, and interact with them. We also learned how to wait for elements, take screenshots, and close the browser. With Selenium, you can automate many web-based tasks. Great work!
pip install seleniumdriver = webdriver.Chrome()driver.get(url)driver.find_element(by, value)element.send_keys(text)element.click()driver.save_screenshot(file)driver.quit()find_element() and find_elements()?WebDriverWait used for?______________ to start a Chrome browser. (webdriver.Chrome())______________ to open a website. (driver.get(url))______________ to type text into an element. (send_keys)______________ to click an element. (click)find_element() finds one element. (True)send_keys() is used to type text. (True)driver.quit() closes the browser. (True)pip install selenium โ
install seleniumpip get seleniumwebdriver.Chrome() โ
start.browser()open.browser()driver.get(url) โ
driver.open(url)driver.navigate(url)driver.find_element(by, value) โ
driver.find(by, value)driver.element(by, value)element.send_keys(text) โ
element.type(text)element.enter(text)element.click() โ
element.press()element.select()driver.save_screenshot(file) โ
driver.screenshot(file)driver.capture(file)WebDriverWait โ
time.sleep()wait_for_element()driver.quit() โ
driver.close()driver.stop()find_elements()?Match the word with its meaning:
| Word | Meaning |
|---|---|
| 1. Selenium | A) Opens a website |
| 2. WebDriver | B) Closes the browser |
| 3. driver.get() | C) A tool for automating browsers |
| 4. send_keys() | D) Types text into an element |
| 5. driver.quit() | E) A program that controls the browser |
(Answers: 1-C, 2-E, 3-A, 4-D, 5-B)
Scenario 1: You want to automate logging into your email account. Write a program that opens the login page, enters your username and password, and clicks the login button.
Scenario 2: You want to take a screenshot of a weather website every day. Write a program that opens the website and saves a screenshot.
In groups of 3, create a program that automates a common web task (like checking the weather, getting news, or checking grades).
Write a program that automates logging into a website of your choice. Take a screenshot after login.
Create a program that automates a complete workflow: login, extract data, take a screenshot, and save the data to a file.
Write a program that automates filling a form on a website. Include at least 5 fields.
Write a program that logs into a website, navigates to a specific page, extracts data, and saves it to a CSV file.
Fill-in-the-blank: 1-web browsers, 2-webdriver.Chrome(), 3-driver.get(url), 4-send_keys, 5-click
True/False: 1-T, 2-F, 3-T, 4-T, 5-T
Matching: 1-C, 2-E, 3-A, 4-D, 5-B
driver.get() to open websites.find_element() to find elements.send_keys() to type text.click() to click buttons.WebDriverWait to wait for elements.driver.quit().In Module Eleven, we will learn about scheduling scripts to run automatically. We will learn how to use Task Scheduler and cron to run your Python scripts at specific times. Before the next session, think of a script you would like to schedule.
See you in Module Eleven! ๐
End of Module Ten ยท Automating Browser Tasks with Selenium