Introduction
Selenium is a strong web browser automation tool that is frequently used for online testing, scraping, and automating tedious operations. In this tutorial, we’ll show you how to install Selenium in Python so you can start taking use of its features and increasing your productivity. Regardless of your level of Python development experience, this step-by-step tutorial will get you up and running with Selenium in no time.
Prerequisites
Make sure your computer has the necessary components installed before we start:
Check to see if Python is installed. On the official Python website (https://www.python.org/downloads/), you can download the most recent version.
Step 1: Configuring the Python Environment:
Create a virtual environment for your Selenium project to get started. Enter the following commands at the command prompt or in your terminal:
# Install virtualenv if you haven't already
pip install virtualenv
# Create a virtual environment named 'seleniumvenv'
virtualenv seleniumvenv
Activate the virtual environment:
For Windows:
seleniumvenv\Scripts\activate
For macOS/Linux:
source seleniumvenv/bin/activate
Sept 2: Installing Selenium:
We can now install Selenium because the virtual environment is now operational. To use Pip to install the Selenium package, enter the following command:
pip install selenium
Step 3:Installation of WebDriver:
Selenium communicates with browsers using WebDriver, which serves as a link between the Python language bindings and the browser. WebDriver is available for different browsers like Chrome, Firefox, Safari, and more. Let’s set up the WebDriver for your preferred browser.
For Chrome:
# Install ChromeDriver
pip install chromedriver-autoinstaller
For Firefox:
# Install GeckoDriver
pip install webdrivermanager
webdrivermanager firefox --linkpath /path/to/directory
Note: Replace “/path/to/directory” with the directory where you want to store the WebDriver executable.
Step 4: Verify the installation:
Let’s execute a basic Selenium script that launches a browser window to verify that everything is configured properly. Add the following code to a new Python file, such as selenium_demo.py:
# Replace 'chrome' with 'firefox' if using Firefox
driver = webdriver.Chrome()
driver.get("https://mrwhotheengineer.com")
Save the file and execute it using Python:
python selenium_demo.py
If everything is installed correctly, a new Chrome (or Firefox) window should open, and it will navigate to “https://mrwhotheengineer.com”.