Python Selenium Tutorial - Microsoft Edge Setup

3 min read 6 months ago
Published on Aug 17, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial will guide you through the process of setting up the Selenium WebDriver with Microsoft Edge. Selenium is a powerful tool for automating web browsers, and integrating it with Edge allows you to run automated tests and web scraping tasks seamlessly. By the end of this tutorial, you will have a fully functional environment to start automating your web tasks with Python and Microsoft Edge.

Step 1: Install Python

To use Selenium with Microsoft Edge, you need to have Python installed on your computer.

  1. Visit the official Python website: python.org.
  2. Download the latest version of Python suitable for your operating system.
  3. Run the installer and ensure you check the box that says "Add Python to PATH" during installation.
  4. Verify the installation by opening a command prompt and typing:
    python --version
    
    You should see the installed version of Python.

Step 2: Install Selenium Package

Next, you need to install the Selenium package using pip, Python's package installer.

  1. Open a command prompt.
  2. Run the following command to install Selenium:
    pip install selenium
    

Step 3: Download Microsoft Edge WebDriver

Selenium requires a WebDriver to interact with web browsers. For Microsoft Edge, you will need to download the Edge WebDriver.

  1. Go to the Microsoft Edge WebDriver download page: Edge WebDriver.
  2. Download the version that matches your installed version of Microsoft Edge.
  3. Extract the downloaded file to a known directory on your computer, for example, C:\WebDriver.

Step 4: Set Up Environment Variables

To make the WebDriver accessible from anywhere in your command line, you need to add it to your system's PATH.

  1. Right-click on "This PC" or "My Computer" and select "Properties".
  2. Click on "Advanced system settings".
  3. In the System Properties window, click on the "Environment Variables" button.
  4. In the Environment Variables window, under "System variables", find and select the "Path" variable, then click "Edit".
  5. Click "New" and add the path to the directory where you extracted the Edge WebDriver (e.g., C:\WebDriver).
  6. Click OK to close all dialog boxes.

Step 5: Write a Sample Automation Script

Now that you have everything set up, you can write a simple Python script to test your configuration.

  1. Open your preferred text editor or IDE.

  2. Create a new Python file, e.g., test_edge.py.

  3. Write the following code to open a website using Microsoft Edge:

    from selenium import webdriver
    
    # Set up Edge options
    options = webdriver.EdgeOptions()
    options.use_chromium = True
    
    # Initialize the Edge driver
    driver = webdriver.Edge(options=options)
    
    # Open a website
    driver.get("https://www.example.com")
    
    # Close the browser after a few seconds
    driver.implicitly_wait(5)  # Wait for 5 seconds
    driver.quit()
    
  4. Save the file and run it from the command prompt:

    python test_edge.py
    

Conclusion

You have successfully set up Selenium with Microsoft Edge! You can now start automating your web tasks using Python. Consider exploring more advanced features of Selenium, such as handling alerts, navigating between pages, and interacting with web elements. Happy coding!