0
0
Selenium-pythonHow-ToBeginner · 4 min read

How to Install ChromeDriver for Selenium Testing

To install ChromeDriver for Selenium, download the matching version from the official chromedriver.chromium.org site, then place the executable in a known folder and add its path to your system environment variables or specify it directly in your Selenium script. This allows Selenium to control the Chrome browser during automated tests.
📐

Syntax

The basic syntax to use ChromeDriver in Selenium involves setting the path to the chromedriver executable and creating a WebDriver instance for Chrome.

  • webdriver.Chrome(executable_path='path/to/chromedriver'): Initializes Chrome browser automation.
  • driver.get('url'): Opens the specified URL in Chrome.
python
from selenium import webdriver

# Set path to chromedriver executable
driver = webdriver.Chrome(executable_path='/path/to/chromedriver')

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

# Close the browser
driver.quit()
💻

Example

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

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

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

# Create a Chrome WebDriver instance
driver = webdriver.Chrome(service=service)

# Open Google homepage
driver.get('https://www.google.com')

# Print page title
print(driver.title)

# Close the browser
driver.quit()
Output
Google
⚠️

Common Pitfalls

Common mistakes when installing ChromeDriver include:

  • Downloading a ChromeDriver version that does not match your installed Chrome browser version.
  • Not setting the executable path correctly, causing Selenium to fail to find ChromeDriver.
  • Not giving executable permissions to the ChromeDriver file on Unix-based systems.
  • Forgetting to close the browser after tests, which can leave processes running.

Always check your Chrome version by navigating to chrome://settings/help and download the matching ChromeDriver version.

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

# WRONG: Incorrect path or missing executable
# service = Service('/wrong/path/chromedriver')
# driver = webdriver.Chrome(service=service)

# RIGHT: Correct path and permissions
service = Service('/correct/path/chromedriver')
driver = webdriver.Chrome(service=service)

# Use driver as usual
📊

Quick Reference

Summary tips for installing ChromeDriver:

  • Check your Chrome browser version first.
  • Download matching ChromeDriver from official site.
  • Place the executable in a folder included in your system PATH or specify the full path in your code.
  • On Linux/Mac, run chmod +x chromedriver to make it executable.
  • Use Selenium's Service class to specify the driver path in modern Selenium versions.

Key Takeaways

Download ChromeDriver version that matches your installed Chrome browser version.
Set the ChromeDriver executable path correctly in your Selenium script or system PATH.
Use Selenium's Service class to specify the ChromeDriver path for better compatibility.
Ensure ChromeDriver has executable permissions on Unix-based systems.
Always close the browser after tests to free system resources.