0
0
Selenium Pythontesting~5 mins

WebDriver setup (ChromeDriver, GeckoDriver) in Selenium Python

Choose your learning style9 modes available
Introduction

We use WebDriver to control web browsers automatically for testing. Setting up ChromeDriver or GeckoDriver lets Selenium open and control Chrome or Firefox browsers.

When you want to test how your website works in Google Chrome.
When you want to test your web app in Mozilla Firefox.
When you need to automate browser actions like clicking buttons or filling forms.
When you want to run automated tests on different browsers to check compatibility.
Syntax
Selenium Python
from selenium import webdriver

# For Chrome
driver = webdriver.Chrome(service=Service('path/to/chromedriver'))

# For Firefox
driver = webdriver.Firefox(service=Service('path/to/geckodriver'))

Replace 'path/to/chromedriver' or 'path/to/geckodriver' with the actual file path on your computer.

Make sure the driver version matches your browser version for smooth operation.

Examples
This example shows how to start Chrome browser using ChromeDriver located at '/usr/local/bin/chromedriver'.
Selenium Python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service

driver = webdriver.Chrome(service=Service('/usr/local/bin/chromedriver'))
This example shows how to start Firefox browser using GeckoDriver on Windows at 'C:\drivers\geckodriver.exe'.
Selenium Python
from selenium import webdriver
from selenium.webdriver.firefox.service import Service

driver = webdriver.Firefox(service=Service('C:\\drivers\\geckodriver.exe'))
Sample Program

This script opens Chrome browser, navigates to example.com, and prints the page title. It uses the recommended Service object to set up ChromeDriver.

Selenium Python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service

# Setup ChromeDriver service
service = Service(executable_path='chromedriver')

# Start Chrome browser
with webdriver.Chrome(service=service) as driver:
    driver.get('https://example.com')
    print(driver.title)
OutputSuccess
Important Notes

Always download the latest driver from official sites: ChromeDriver from https://chromedriver.chromium.org/ and GeckoDriver from https://github.com/mozilla/geckodriver/releases.

On some systems, you can add the driver to your system PATH to avoid specifying the full path.

Use the Service class from selenium.webdriver.chrome.service or selenium.webdriver.firefox.service for better driver management.

Summary

WebDriver setup connects Selenium to browsers using drivers like ChromeDriver or GeckoDriver.

Specify the driver path correctly and match driver version with your browser.

Use the Service class for cleaner and modern driver setup in Selenium Python.