0
0
Selenium-pythonHow-ToBeginner ยท 4 min read

How to Install WebDriver for Selenium: Step-by-Step Guide

To install WebDriver for Selenium, download the browser-specific driver (like ChromeDriver for Chrome) from its official site and place it in a known folder. Then, configure your Selenium script to use this driver by specifying its path or adding it to your system's PATH environment variable.
๐Ÿ“

Syntax

Here is the general syntax to set up WebDriver in your Selenium script:

  • driver = webdriver.Chrome(service=Service('path/to/chromedriver')) - For Chrome browser
  • driver = webdriver.Firefox(service=Service('path/to/geckodriver')) - For Firefox browser
  • driver = webdriver.Edge(service=Service('path/to/edgedriver')) - For Edge browser

The service parameter points to the downloaded WebDriver executable wrapped in a Service object.

python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service

# Example for Chrome
path = '/path/to/chromedriver'
service = Service(path)
driver = webdriver.Chrome(service=service)
driver.get('https://www.example.com')
driver.quit()
๐Ÿ’ป

Example

This example shows how to download ChromeDriver, set it up, and run a simple Selenium test that opens a webpage and closes the browser.

python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service

# Specify the path to the ChromeDriver executable
service = Service('/path/to/chromedriver')

# Create a new Chrome session
driver = webdriver.Chrome(service=service)

# Open a website
driver.get('https://www.example.com')

# Print the page title
print(driver.title)

# Close the browser
driver.quit()
Output
Example Domain
โš ๏ธ

Common Pitfalls

Common mistakes when installing WebDriver include:

  • Downloading the wrong driver version that does not match your browser version.
  • Not setting the driver path correctly, causing Selenium to fail to find the driver.
  • Forgetting to add the driver to your system PATH, which can simplify usage.
  • Using outdated drivers incompatible with the latest Selenium or browser versions.

Always verify your browser version and download the matching WebDriver version.

python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service

# Wrong way: Not specifying the correct path or driver version
# driver = webdriver.Chrome()  # May fail if driver not in PATH or version mismatch

# Right way: Specify correct path and matching driver version
service = Service('/correct/path/to/chromedriver')
driver = webdriver.Chrome(service=service)
๐Ÿ“Š

Quick Reference

BrowserDriver NameDownload URLNotes
Google ChromeChromeDriverhttps://sites.google.com/chromium.org/driver/Match Chrome version
Mozilla FirefoxGeckoDriverhttps://github.com/mozilla/geckodriver/releasesMatch Firefox version
Microsoft EdgeEdgeDriverhttps://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/Match Edge version
โœ…

Key Takeaways

Download the WebDriver that matches your browser version exactly.
Specify the WebDriver executable path in your Selenium script or add it to your system PATH.
Keep your WebDriver updated to avoid compatibility issues.
Use official sources to download WebDriver executables to ensure safety and correctness.
Test your setup by opening a simple webpage and checking for errors.