Skip to main content

Selenium is a robust framework for automating web browser interactions, and it’s particularly useful for web testing and automation tasks. In this article, we’ll provide a comprehensive introduction to Selenium and walk you through a Python script that extracts text from a Wikipedia page.

Understanding Selenium

Selenium allows developers and testers to automate interactions with web browsers, simulating user actions like clicking buttons, filling forms, and navigating through web pages. It supports multiple programming languages, and in this guide, we’ll focus on using Selenium with Python.

Setting Up Your Environment

Before diving into Selenium, ensure you have Python installed on your system. To add Selenium to your Python environment, run the following command in your terminal or command prompt:

pip install selenium

Additionally, download the WebDriver for the browser you intend to use. In our example, we’re using Firefox, so you’ll need to download GeckoDriver from the official Mozilla GeckoDriver releases page.

The Python Script

Now, let’s break down the Python script that uses Selenium to navigate to a Wikipedia page and extract its content.

Breaking Down the Code

  1. Importing Libraries:
    • from selenium import webdriver: Import the Selenium WebDriver module for controlling a web browser.
    • from selenium.webdriver.common.by import By: Import the By class for specifying how to locate elements on a web page.
  2. Creating a WebDriver Instance:
    • driver = webdriver.Firefox(): Create a new instance of the Firefox driver. You can replace this with webdriver.Chrome() for Chrome.
  3. Navigating to a Web Page:
    • driver.get("https://en.wikipedia.org/wiki/Selenium_(software)"): Open the specified URL in the browser.
  4. Getting and Printing the Webpage Title:
    • title = driver.title: Get the title of the webpage.
    • print("Title of the webpage:", title): Print the webpage title to the console.
  5. Locating and Extracting Text from an Element:
    • content_element = driver.find_element(By.ID, "mw-content-text"): Find the main content element on the page using its ID.
    • page_content = content_element.text: Get the text content of the main content element.
  6. Printing Extracted Text:
    • print(page_content): Print the extracted text to the console.
  7. Closing the Browser Window:
    • driver.quit(): Close the browser window.

Conclusion

By understanding this script, you’ve taken your first steps into the world of Selenium automation. Feel free to modify and expand upon this code for your specific needs, and explore additional features offered by Selenium for more advanced web automation tasks.

Happy coding with Selenium!

Close Menu

Welcome to Coded Brainy

Coded Brainy Provides you with technical blogs

Coded Brainy