We use WebDriver to control web browsers automatically for testing. Setting up ChromeDriver or GeckoDriver lets Selenium open and control Chrome or Firefox browsers.
WebDriver setup (ChromeDriver, GeckoDriver) in 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.
from selenium import webdriver from selenium.webdriver.chrome.service import Service driver = webdriver.Chrome(service=Service('/usr/local/bin/chromedriver'))
from selenium import webdriver from selenium.webdriver.firefox.service import Service driver = webdriver.Firefox(service=Service('C:\\drivers\\geckodriver.exe'))
This script opens Chrome browser, navigates to example.com, and prints the page title. It uses the recommended Service object to set up ChromeDriver.
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)
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.
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.