Python Selenium Tutorial - Microsoft Edge Setup
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.
- Visit the official Python website: python.org.
- Download the latest version of Python suitable for your operating system.
- Run the installer and ensure you check the box that says "Add Python to PATH" during installation.
- Verify the installation by opening a command prompt and typing:
You should see the installed version of Python.python --version
Step 2: Install Selenium Package
Next, you need to install the Selenium package using pip, Python's package installer.
- Open a command prompt.
- 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.
- Go to the Microsoft Edge WebDriver download page: Edge WebDriver.
- Download the version that matches your installed version of Microsoft Edge.
- 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.
- Right-click on "This PC" or "My Computer" and select "Properties".
- Click on "Advanced system settings".
- In the System Properties window, click on the "Environment Variables" button.
- In the Environment Variables window, under "System variables", find and select the "Path" variable, then click "Edit".
- Click "New" and add the path to the directory where you extracted the Edge WebDriver (e.g.,
C:\WebDriver
). - 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.
-
Open your preferred text editor or IDE.
-
Create a new Python file, e.g.,
test_edge.py
. -
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()
-
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!