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.
Practice
Solution
Step 1: Understand WebDriver role
WebDriver acts as a bridge between Selenium scripts and the browser.Step 2: Identify driver purpose
ChromeDriver and GeckoDriver are specific drivers to control Chrome and Firefox browsers respectively.Final Answer:
To connect Selenium scripts with the browser for automation -> Option AQuick Check:
Driver connects Selenium to browser = B [OK]
- Thinking drivers write test cases
- Confusing drivers with reporting tools
- Assuming drivers manage databases
Solution
Step 1: Check import statements
Correct import for Service class is from selenium.webdriver.chrome.service import Service.Step 2: Verify driver initialization
Driver must be created with webdriver.Chrome(service=service) passing the Service object.Final Answer:
from selenium import webdriver from selenium.webdriver.chrome.service import Service service = Service('path/to/chromedriver') driver = webdriver.Chrome(service=service) -> Option AQuick Check:
Use Service class and pass to Chrome() = A [OK]
- Passing driver path directly to Chrome() without Service
- Wrong import paths for Service
- Using incorrect class names like ChromeDriver
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service = Service('wrong/path/chromedriver')
driver = webdriver.Chrome(service=service)
driver.get('https://example.com')Solution
Step 1: Analyze driver path usage
Service is given a wrong path to ChromeDriver executable.Step 2: Understand error on invalid path
Selenium cannot find the driver file, so it raises an error like FileNotFoundError or WebDriverException.Final Answer:
A FileNotFoundError or WebDriverException is raised due to wrong driver path -> Option CQuick Check:
Wrong driver path causes error = D [OK]
- Assuming driver auto-downloads
- Expecting browser to open without driver
- Thinking code runs but page stays blank
from selenium import webdriver
service = Service('path/to/geckodriver')
driver = webdriver.Firefox(service=service)
driver.get('https://example.com')Solution
Step 1: Check imports
Service class for Firefox must be imported from selenium.webdriver.firefox.service.Step 2: Verify usage
Without importing Service, the code will raise NameError or similar.Final Answer:
Missing import of Service class from selenium.webdriver.firefox.service -> Option BQuick Check:
Import Service for Firefox driver = C [OK]
- Not importing Service class
- Wrong path format causing silent failure
- Calling driver.get() before driver creation
Solution
Step 1: Understand driver-browser compatibility
GeckoDriver must match the Firefox browser version to work properly.Step 2: Apply best practice for updates
Download the correct GeckoDriver version for Firefox 115 and update your code to use its path.Final Answer:
Download the GeckoDriver version compatible with Firefox 115 and update the Service path accordingly -> Option DQuick Check:
Match driver version with browser version = A [OK]
- Assuming Selenium auto-updates drivers
- Switching browsers unnecessarily
- Passing driver path without Service class
Ready to prove it?
Pass the test and this pattern moves unknown
