In this Python tutorial, you will learn how to install the Selenium Webdriver on your device, regardless of your operating system or browser.
Selenium is a powerful open-source framework that is widely used for automating web browsers. It provides a set of tools and libraries that enable developers to interact with web applications in a programmatic way. Selenium allows you to write test scripts in various programming languages, including Python, Java, C#, and more. By automating browser actions, you can perform tasks such as web scraping, form submission, and functional testing.
Selenium supports multiple web browsers like Chrome, Firefox, Safari, and Internet Explorer, making it a versatile choice for web automation. It provides a range of features, including element identification, navigation, form interaction, and more. With Selenium, you can simulate user actions and extract data from websites effortlessly.
What is the “Webdriver” in Selenium (Python)
In Selenium, the “webdriver” acts as a bridge between your code and the web browser. It facilitates communication with the browser and enables you to control its behavior programmatically. The webdriver provides a set of methods and properties that allow you to interact with web elements, simulate user actions, and retrieve information from web pages.
The webdriver is responsible for launching the browser, loading web pages, and executing actions such as clicking buttons, filling out forms, and extracting data. It acts as a driver that navigates through the web and performs the operations you specify in your code. Without the webdriver, you would not be able to automate browser tasks using Selenium.
Four methods of setting up Webdriver
There are multiple methods available to set up the webdriver in Python for Selenium. Each method has its own advantages and considerations. Let’s explore these methods briefly:
1. Selenium Manager (beta): Selenium Manager is a beta feature introduced in the Selenium 4.6.0 (Nov. 2022). It provides a centralized location for managing web drivers, eliminating the need for manual downloads and setup. It simplifies the process of handling web drivers and ensures compatibility with the installed Selenium version.
However, as it is still in beta, it might have limitations or potential issues.
2. Webdriver_manager module: The webdriver_manager module is a Python package that simplifies the management of web drivers. It automatically downloads the required webdriver binaries for the specified browser version, eliminating the need for manual downloads. This method is easy to set up and ensures that you have the correct webdriver version at all times.
However, it requires an additional package installation.
3. Manual Driver Download with Hard Coded Location: With this method, you manually download the webdriver executable file and specify its location in your code. This approach gives you control over the webdriver version and location.
However, it requires you to manually update the webdriver when a new version is released, and you need to ensure that the correct version is compatible with your browser.
4. Manual Driver Download with PATH: Similar to the previous method, you manually download the webdriver executable file, but instead of hard-coding its location, you add the webdriver to your system’s PATH environment variable. By doing so, you enable your code to locate the webdriver without explicitly specifying its location.
This method simplifies the setup process and ensures that your code can find the webdriver regardless of its location. However, you still need to manually update the webdriver when necessary.
In the following sections, we will dive deeper into each method and provide step-by-step instructions on how to install the Selenium webdriver in Python using each one of these approaches.
Method 1: Selenium Manager (beta)
Selenium Manager is a beta feature introduced in Selenium 4.6.0 (Nov. 2022) that simplifies the management of web drivers. It provides a centralized location for handling web drivers, eliminating the need for manual downloads and setup. You also do not have to download any extra dependencies. This makes selenium a “batteries-included” library, which is previously was not.
So you might wondering, how to use this “selenium manager”?
You don’t!
The selenium manager is included and enabled by default, and will attempt to locate the correct drivers after automatically detecting the browsers you have. Just run your selenium code normally, but this time without any extra boiler plate code.
Be warned however, this is still a beta feature, and may not work under certain scenarios, or with certain browser types or browser versions. You also may not have Selenium version 4.6.0 or higher.
In case any of the above applies, any of three methods below can be used.
Method 2: Webdriver_manager module
The webdriver_manager module is a convenient option for managing webdrivers in Selenium. It automatically handles the downloading and installation of the required webdriver binaries. Here’s how you can set it up:
Install the webdriver_manager package by running the following command in your command prompt or terminal:
pip install webdriver_manager
Import the necessary modules in your Python script:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService
Initialize the webdriver using the webdriver_manager and specify the browser you want to use. In this example, we’ll use Chrome:
service = ChromeService(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)
This line of code will automatically download the appropriate Chrome webdriver and set it up for you.
Now, you can start automating the browser using the driver
object. For example, you can open a web page:
driver.get("https://www.example.com")
Congratulations! You have successfully set up the webdriver using the webdriver_manager module.
Next we will show you the code required to setup webdriver_manager for some other popular browsers, like Firefox and Opera. Please keep in mind that the below code is for the newer Selenium (version 4).
If you have an older version, or you have a browser which we have not listed here, visit the github page of the webdriver_manager, where you will find a complete list of codes, for older version and lesser known browsers.
Firefox
from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager
from selenium.webdriver.firefox.service import Service as FirefoxService
service = FirefoxService(executable_path=GeckoDriverManager().install())
driver = webdriver.Firefox(service=service)
Opera
from selenium import webdriver
from selenium.webdriver.chrome import service
from webdriver_manager.opera import OperaDriverManager
webdriver_service = service.Service(OperaDriverManager().install())
webdriver_service.start()
options = webdriver.ChromeOptions()
options.add_experimental_option('w3c', True)
driver = webdriver.Remote(webdriver_service.service_url, options=options)
Microsoft Edge
from selenium import webdriver
from selenium.webdriver.edge.service import Service as EdgeService
from webdriver_manager.microsoft import EdgeChromiumDriverManager
service = EdgeService(EdgeChromiumDriverManager().install())
driver = webdriver.Edge(service=service)
Method 3: Manual Driver Download with Hard Coded Location
In this method, you manually download the webdriver executable file and specify its location in your code. Follow the steps below:
First, manually download the webdriver executable for your desired browser version. You can find the appropriate webdriver for each browser from the official Selenium website or the respective browser driver documentation. Alternatively, here are the links to the web driver download pages for some popular browsers.
Place the downloaded webdriver executable in a directory of your choice.
Import the necessary modules in your Python script:
from selenium import webdriver
Specify the location of the webdriver executable in your code when initializing the webdriver. In this example, we’ll use the Chrome webdriver:
driver = webdriver.Chrome(executable_path="path/to/chromedriver")
Replace "path/to/chromedriver"
with the actual path to the webdriver executable file. Alternatively, for other browsers, simply replace the “Chrome” function with the name of the browser. Here are some sample codes for other browsers.
# Edge
driver = webdriver.Edge(executable_path="path/to/edgedriver")
# Firefox
driver = webdriver.Firefox(executable_path="path/to/firefoxdriver")
# Internet Explorer
driver = webdriver.Ie(executable_path="path/to/internetexplorerdriver")
Once the driver object has been created, you can now start automating the browser using the driver
object:
driver.get("https://www.example.com")
Great job! You have successfully set up the webdriver manually with a hard-coded location.
Method 4: Manual Driver Download with PATH
The annoying thing about “Method 3” is that you need to use the hard-code the file path of your web driver. This can cause issues when moving your files around, or when deploying your software to other PC’s, where having hard-coded paths is a recipe for disaster.
Instead, what we can do is add our download webdriver to PATH (environment variables). Selenium automatically checks the PATH if you do not provide it with an explicit file path. For those who do not know, “PATH” is where special file paths for certain software, so that they can be accessed from anywhere in the system (e.g the Python interpreter software).
We will assume that you have already download the webdriver (as discussed in Method 3).
Now all you have to do is place this webdriver in a folder, e.g: “Webdriver” and then add this folder to PATH. You can also choose to place the webdriver in a folder that already exists on PATH. The method of adding a new environment variable to PATH varies from OS to OS, but here is how to do in on Windows/Linux.
Run the following commands in the terminal for:
Windows
setx PATH "%PATH%;C:\WebDriver\bin"
Linux
echo 'export PATH=$PATH:/path/to/driver' >> ~/.bash_profile
source ~/.bash_profile
This marks the end of the How to Install Selenium Webdriver in Python? Tutorial. Any suggestions or contribution for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.