In this Python tutorial, we will learn how to install Selenium for Chrome using the Webdriver_manager module.
Selenium is a popular open-source framework used for automating web browsers. It provides a convenient way to interact with web elements, perform actions, and extract data from websites. To use Selenium with Python, you need to have the appropriate WebDriver installed for the browser you wish to automate.
One of the challenges with Selenium is managing the WebDriver executables and keeping them up to date. WebDriver executables are required to establish a connection between Selenium and the web browser. They vary depending on the browser and the operating system you are using.
Fortunately, there is a handy Python library called
webdriver_manager that simplifies the process of managing WebDriver executables. It automatically downloads the required webdriver executables by detecting the installed browsers you have. All we need is a few extra lines of code in our python programs.
In this article, we will guide you through the installation process of
webdriver_manager for Chrome in Selenium Python.
Install Selenium for Chrome using Webdriver_manager in Python
We will be explaining how to setup your Python, Selenium, and Webdriver_manager environments in the following steps. You may already have some of these installed (e.g Python or Selenium), so skip the steps you have already completed.
Step 1: Set up a Python environment
Before we begin, make sure you have Python installed on your system. You can download and install Python from the official Python website (https://www.python.org/) if you haven’t already. It is recommended to use Python version 3.6 or above for compatibility with newer Selenium versions.
You can check your Python version using the following command:
Step 2: Install Selenium
Next, you need to install the Selenium Python package. Open a terminal or command prompt and run the following command:
pip install selenium
This command will download and install the latest version of Selenium from the Python Package Index (PyPI).
Step 3: Install
Once Selenium is installed, you can proceed to install
webdriver_manager by running the following command:
pip install webdriver_manager
This command will fetch the
webdriver_manager package from PyPI and install it in your Python environment.
Step 4: Import
webdriver_manager and use it with Selenium
Now that you have installed
webdriver_manager, you can import it in your Python script and use it to manage WebDriver executables. The syntax and functions involved are bit different based on the browser you are planning to use.
Here is the code for using the webdriver_manager in Python Selenium for Chrome.
First we make the required imports. There are some additional imports here needed from the
from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.chrome.service import Service as ChromeService from selenium import webdriver
Next, we need to create the “driver” object which we will be using to extract and parse data from websites. We are using the ChromeDriverManager import here from the webdriver_manager module to automatically download and install the required files.
service = ChromeService(executable_path=ChromeDriverManager().install()) driver = webdriver.Chrome(service=service)
Now we can begin extracting data from websites, by making a “get” call to the URL of your choice.
Once this “get” method has been called, we now have access to all the raw HTML data from the URL passed into this
Finally, we will call the
quit() method on the driver object to close the browser window.
If you do not want the browser window to appear, you can activate “headless” mode. This keeps the selenium driver running “under the hood”. Other than the obvious benefit of not spawning an annoying window each time, “headless” mode is also faster and more efficient.
This marks the end of the How to Install Webdriver_manager Chrome in Selenium Python? article. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.