How to Set Up Selenium with Chrome for Web Testing
To set up
Selenium with Chrome, install the Selenium package and download the ChromeDriver executable matching your Chrome version. Then, initialize webdriver.Chrome() in your test script pointing to the ChromeDriver path to start automated browser control.Syntax
This is the basic syntax to start Chrome browser using Selenium WebDriver in Python:
from selenium import webdriver: Imports Selenium WebDriver module.driver = webdriver.Chrome(service=Service('path_to_chromedriver')): Creates a Chrome browser instance using the ChromeDriver executable.driver.get('url'): Opens the specified URL in the browser.driver.quit(): Closes the browser and ends the session.
python
from selenium import webdriver from selenium.webdriver.chrome.service import Service driver = webdriver.Chrome(service=Service('path_to_chromedriver')) driver.get('https://www.example.com') driver.quit()
Example
This example shows how to open Chrome, navigate to a website, check the page title, and close the browser.
python
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By # Set path to chromedriver executable service = Service('chromedriver') # Create Chrome WebDriver instance with webdriver.Chrome(service=service) as driver: driver.get('https://www.example.com') title = driver.title print(f'Page title is: {title}')
Output
Page title is: Example Domain
Common Pitfalls
Common mistakes when setting up Selenium with Chrome include:
- Using a ChromeDriver version that does not match your installed Chrome browser version.
- Not specifying the correct path to the ChromeDriver executable.
- Forgetting to close the browser with
driver.quit(), which can leave processes running. - Not installing Selenium package before running the script.
Always download the ChromeDriver from the official site matching your Chrome version and keep Selenium updated.
python
from selenium import webdriver from selenium.webdriver.chrome.service import Service # Wrong: Missing Service object and path # driver = webdriver.Chrome() # Right: Specify Service with correct path service = Service('chromedriver') driver = webdriver.Chrome(service=service) driver.quit()
Quick Reference
| Step | Description |
|---|---|
| Install Selenium | Run 'pip install selenium' to install the Selenium package. |
| Download ChromeDriver | Get ChromeDriver matching your Chrome version from https://chromedriver.chromium.org/downloads |
| Set ChromeDriver Path | Provide the path to ChromeDriver in your script using Service or executable_path. |
| Initialize WebDriver | Create a Chrome WebDriver instance to control the browser. |
| Run Tests | Use WebDriver methods like get(), find_element(), quit() to automate browser actions. |
Key Takeaways
Always match ChromeDriver version with your installed Chrome browser version.
Use Selenium's Service class to specify the ChromeDriver executable path.
Remember to close the browser with driver.quit() to free resources.
Install Selenium package via pip before running your scripts.
Test your setup by opening a website and checking the page title.