0
0
Selenium-pythonHow-ToBeginner ยท 4 min read

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

StepDescription
Install SeleniumRun 'pip install selenium' to install the Selenium package.
Download ChromeDriverGet ChromeDriver matching your Chrome version from https://chromedriver.chromium.org/downloads
Set ChromeDriver PathProvide the path to ChromeDriver in your script using Service or executable_path.
Initialize WebDriverCreate a Chrome WebDriver instance to control the browser.
Run TestsUse 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.