Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
โ† Python For Automation ยท Lesson 12 of 14

Module Ten

๐Ÿ“– Every lesson in this course is free to read right here, no account needed. Create a free account to track your progress, take the exam, and earn your certificate.
12
Module Ten ยท Python for Automation

๐Ÿ–ฅ๏ธ Module Ten ยท Automating Browser Tasks with Selenium


๐Ÿ“– Module Introduction

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.

๐ŸŽฏ Learning Objectives

  • Understand what Selenium is and why it is useful.
  • Install Selenium and set up a web driver.
  • Open websites and interact with them.
  • Click buttons, fill forms, and login automatically.
  • Take screenshots and extract data using Selenium.

๐Ÿ“š Warm-up Story ยท Grace's Automated Login

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!

๐Ÿงฉ Main Lessons

Lesson 1 ยท What is Selenium?

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.


Lesson 2 ยท Installing Selenium

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.


Lesson 3 ยท Starting a Browser

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.


Lesson 4 ยท Opening a Website

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.


Lesson 5 ยท Finding Elements

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.


Lesson 6 ยท Interacting with 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.


Lesson 7 ยท Automating Login

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.


Lesson 8 ยท Filling Forms

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().


Lesson 9 ยท Taking Screenshots

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.


Lesson 10 ยท Waiting for Elements

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.


Lesson 11 ยท Closing the Browser

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.


Lesson 12 ยท Using Selenium in Automation

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.


Lesson 13 ยท You Are Ready!

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!


๐Ÿ“Œ Key Vocabulary

  • Selenium: A tool for automating web browsers.
  • Web Driver: A program that controls the browser.
  • Element: A part of a web page (button, input, etc.).
  • Click: To press a button.
  • Send keys: To type text into an input.
  • Screenshot: An image of the browser window.
  • Wait: To pause until an element is ready.
  • Quit: To close the browser.
  • Automate: To make a task automatic.
  • Browser: A program like Chrome, Firefox, etc.

๐Ÿง  Important Concepts

  • Selenium controls browsers: It can open, click, type, and more.
  • Find elements first: Before interacting, find the element.
  • Wait for elements: Web pages take time to load.
  • Close the browser: Always close the browser after automation.
  • Web automation is powerful: It can save hours of work.

๐Ÿ‘ฃ Step-by-Step Explanations

How to Automate Login with Selenium

  1. Import libraries: from selenium import webdriver
  2. Start the browser: driver = webdriver.Chrome()
  3. Open the login page: driver.get("https://example.com/login")
  4. Find username field: username = driver.find_element("id", "username")
  5. Enter username: username.send_keys("my_username")
  6. Find password field: password = driver.find_element("id", "password")
  7. Enter password: password.send_keys("my_password")
  8. Click login button: login_btn = driver.find_element("id", "login"); login_btn.click()
  9. Wait for page to load: import time; time.sleep(2)
  10. Close browser: driver.quit()

Example:

    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()
            

๐ŸŒ Real-life Examples

  • Login: driver.get("https://example.com/login"); username.send_keys("user"); password.send_keys("pass"); login_btn.click()
  • Search: search = driver.find_element("name", "q"); search.send_keys("Python"); search.submit()
  • Form: name.send_keys("Aisha"); email.send_keys("aisha@email.com"); submit.click()
  • Screenshot: driver.save_screenshot("page.png")
  • Wait: WebDriverWait(driver, 10).until(EC.presence_of_element_located(("id", "username")))

๐Ÿ‡ณ๐Ÿ‡ฌ Nigerian Examples

  • Bank login: driver.get("https://bank.com/login"); username.send_keys("1234567890"); password.send_keys("my_password"); login_btn.click()
  • School portal: driver.get("https://schoolportal.com"); username.send_keys("student_id"); password.send_keys("password"); login_btn.click()
  • E-commerce: search = driver.find_element("name", "search"); search.send_keys("rice"); search.submit()
  • Form: name.send_keys("Chidi"); email.send_keys("chidi@email.com"); submit.click()
  • Screenshot: driver.save_screenshot("transaction.png")

๐ŸŽˆ Fun Examples children can relate to

  • 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()

๐Ÿ  Everyday Examples

  • Email: driver.get("https://gmail.com"); username.send_keys("my_email"); password.send_keys("my_password"); login_btn.click()
  • Shopping: driver.get("https://amazon.com"); search.send_keys("books"); search.submit()
  • News: driver.get("https://bbc.com"); headlines = driver.find_elements("class", "headline"); for headline in headlines: print(headline.text)
  • Weather: driver.get("https://weather.com"); temp = driver.find_element("class", "temp"); print(temp.text)
  • Social media: driver.get("https://twitter.com"); login_btn.click()

๐Ÿ‘ฉโ€๐Ÿซ Teacher Notes

  • Encourage students to automate a simple login task.
  • Use local examples to make it relatable.
  • Discuss the power and responsibility of automation.
  • Have students create programs that automate browser tasks.
  • Celebrate every program they write.

๐Ÿ‘ช Parent Tips

  • Help your child set up Selenium and a web driver.
  • Write programs together that automate simple tasks.
  • Encourage them to think of tasks they can automate.
  • Make learning Python a fun activity.
  • Celebrate their progress.

๐Ÿง Interesting Facts

  • Selenium is used by many companies for testing.
  • Selenium supports multiple browsers.
  • Selenium can automate complex workflows.
  • Web automation saves millions of hours.
  • Selenium is one of the most popular automation tools.

๐Ÿ’ก Did You Know?

  • Did you know that Selenium can run headless (without a visible browser)?
  • Did you know that you can use Selenium with Firefox, Edge, and Safari?
  • Did you know that Selenium can take screenshots of specific elements?
  • Did you know that Selenium can execute JavaScript?
  • Did you know that Selenium is open-source?

๐Ÿงท Remember This

  • Selenium controls web browsers.
  • Find elements before interacting with them.
  • Use send_keys() to type text.
  • Use click() to click buttons.
  • Use WebDriverWait to wait for elements.
  • Always close the browser with driver.quit().

โš ๏ธ Common Mistakes

  • Wrong driver: Not having the correct web driver.
  • Element not found: Using the wrong selector.
  • Not waiting: Trying to interact with elements that haven't loaded.
  • Not closing: Forgetting to close the browser.
  • Wrong URL: Using an incorrect URL.

โœ… Best Practices

  • Use WebDriverWait for loading pages.
  • Use unique selectors (ID, name, etc.).
  • Always close the browser after automation.
  • Handle exceptions gracefully.
  • Test with small scripts first.

๐ŸŽจ Illustrations, Diagrams & Tables

Selenium Flow

    +----------------------+
    | 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()        |
    +----------------------+
            

Common Selenium Methods

MethodDescription
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

Comparison: Selenium vs BeautifulSoup

SeleniumBeautifulSoup
Controls browserParses HTML
Can interact with pagesOnly extracts data
SlowerFaster
Handles JavaScriptDoesn't handle JavaScript

๐Ÿ“ End-of-Module Summary

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!

โ“ Frequently Asked Questions (10)

  1. What is Selenium? A tool for automating web browsers.
  2. How do you install Selenium? pip install selenium
  3. What is a web driver? A program that controls the browser.
  4. How do you start a browser? driver = webdriver.Chrome()
  5. How do you open a website? driver.get(url)
  6. How do you find an element? driver.find_element(by, value)
  7. How do you type text? element.send_keys(text)
  8. How do you click a button? element.click()
  9. How do you take a screenshot? driver.save_screenshot(file)
  10. How do you close the browser? driver.quit()

๐Ÿ“‹ Review Questions (15)

  1. What is Selenium?
  2. How do you install Selenium?
  3. What is a web driver?
  4. How do you start a browser?
  5. How do you open a website?
  6. How do you find an element?
  7. How do you type text into an input field?
  8. How do you click a button?
  9. How do you take a screenshot?
  10. How do you wait for an element?
  11. How do you close the browser?
  12. What is the difference between find_element() and find_elements()?
  13. What is WebDriverWait used for?
  14. Can Selenium handle JavaScript?
  15. What is one common use of Selenium?

โœ๏ธ Fill-in-the-Blank Exercises

  1. Selenium is a tool for automating ______________. (web browsers)
  2. Use ______________ to start a Chrome browser. (webdriver.Chrome())
  3. Use ______________ to open a website. (driver.get(url))
  4. Use ______________ to type text into an element. (send_keys)
  5. Use ______________ to click an element. (click)

โœ… True or False Exercises

  1. Selenium is used to automate web browsers. (True)
  2. You can use Selenium with Chrome only. (False)
  3. find_element() finds one element. (True)
  4. send_keys() is used to type text. (True)
  5. driver.quit() closes the browser. (True)

๐Ÿ”˜ Multiple Choice Questions (15)

  1. What is Selenium?
    a) A tool for automating web browsers โœ…
    b) A tool for web scraping
    c) A tool for sending emails
  2. How do you install Selenium?
    a) pip install selenium โœ…
    b) install selenium
    c) pip get selenium
  3. What is a web driver?
    a) A program that controls the browser โœ…
    b) A web page
    c) A type of website
  4. How do you start a browser?
    a) webdriver.Chrome() โœ…
    b) start.browser()
    c) open.browser()
  5. How do you open a website?
    a) driver.get(url) โœ…
    b) driver.open(url)
    c) driver.navigate(url)
  6. How do you find an element?
    a) driver.find_element(by, value) โœ…
    b) driver.find(by, value)
    c) driver.element(by, value)
  7. How do you type text?
    a) element.send_keys(text) โœ…
    b) element.type(text)
    c) element.enter(text)
  8. How do you click a button?
    a) element.click() โœ…
    b) element.press()
    c) element.select()
  9. How do you take a screenshot?
    a) driver.save_screenshot(file) โœ…
    b) driver.screenshot(file)
    c) driver.capture(file)
  10. How do you wait for an element?
    a) WebDriverWait โœ…
    b) time.sleep()
    c) wait_for_element()
  11. How do you close the browser?
    a) driver.quit() โœ…
    b) driver.close()
    c) driver.stop()
  12. Can Selenium handle JavaScript?
    a) Yes โœ…
    b) No
  13. What is find_elements()?
    a) Finds all matching elements โœ…
    b) Finds one element
    c) Finds no elements
  14. Is Selenium only for testing?
    a) No, it is also used for automation โœ…
    b) Yes, only for testing
  15. Can Selenium automate login?
    a) Yes โœ…
    b) No

๐Ÿ”— Matching Exercises

Match the word with its meaning:

WordMeaning
1. SeleniumA) Opens a website
2. WebDriverB) 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)

๐Ÿ“ Short Answer Questions

  1. What is Selenium and what is it used for?
  2. Write a program that opens Google and searches for "Python".
  3. How do you find an element and click it?

๐ŸŽญ Scenario-based Exercises

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.

๐Ÿ‘ฅ Group Activity

In groups of 3, create a program that automates a common web task (like checking the weather, getting news, or checking grades).

๐Ÿง‘โ€๐ŸŽ“ Individual Activity

Write a program that automates logging into a website of your choice. Take a screenshot after login.

๐Ÿ—ฃ๏ธ Classroom Discussion Questions

  • How can Selenium be used in business?
  • What are some ethical considerations of web automation?
  • What other tasks would you like to automate?

๐Ÿ› ๏ธ Mini Project

Create a program that automates a complete workflow: login, extract data, take a screenshot, and save the data to a file.

๐Ÿ“ Practical Assignment

Write a program that automates filling a form on a website. Include at least 5 fields.

๐Ÿ† Challenge Exercise

Write a program that logs into a website, navigates to a specific page, extracts data, and saves it to a CSV file.

๐Ÿ”‘ Quiz Answers

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

๐ŸŽ Key Takeaways

  • Selenium controls web browsers automatically.
  • Use driver.get() to open websites.
  • Use find_element() to find elements.
  • Use send_keys() to type text.
  • Use click() to click buttons.
  • Use WebDriverWait to wait for elements.
  • Always close the browser with driver.quit().

๐Ÿš€ Preparation for the next module

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

๐Ÿ† Get Certified

๐Ÿ”’

Earn this certificate

Every lesson is already free to read. Sign up, finish the lessons, pass the exam (โ‚ฆ2,000 or 20,000 coins), and get a verified certificate with your name on it.

๐ŸŽ“ Sign Up to Get Certified
๐Ÿ› ๏ธ Practice Tools
Hands-on simulators & labs - subscription required.
โ†’
๐ŸŽฏ Internship Tasks
Real-world tasks to build your portfolio - unlock them with a subscription.
โ†’